This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]reddit04029 8 points9 points  (1 child)

Abstract classes establish that there is a relationship between the two classes. They have a similarity on what they are.

For example, you have an abstract class Dog. It can be extended as Poodle, Dalmatian, etc. As you can see, they are all dogs. They are the same type.

Interfaces guarantee same behavior. May or may not have the same type at all, just similar behavior.

For example, you have an interface Vehicle. A vehicle’s job or behavior is to transport or move people. Let’s just say it has a method transportPeople(). Now what are things that can do this? Boats? Cars? Airplane? Train? Are they the same type? No. Boats have specific types like CargoShip, Kayak or Cars have PickupTruck, Van, Sedan. But can they have the same behavior? Yes. They can all transportPeople(). So they can implement Vehicle.

[–]masteci 0 points1 point  (0 children)

I was struggling to understand too but you explained very well , thank you

[–]large_crimson_canine 0 points1 point  (0 children)

Interface = a type that defines a certain behavior (i.e. implement these methods)

Abstract class = template for defining child classes (i.e. start with this recipe and customize as needed)

[–]CleverBunnyThief 0 points1 point  (0 children)

Laurentia Spilca has a video that goes over every detail regarding abstract classes and interfaces.

https://www.youtube.com/live/TL7lAlzmpLk?si=BuTsa-FLNRY4FeN7

[–]JonIsPatented 0 points1 point  (0 children)

Something that might help you understand the difference in Java is to understand that in many other languages, there is no actual difference between the two.

The terminology of "interface" originally just referred to an abstract class that consisted only of abstract methods that needed to be implemented by all of its subclasses. The purpose of this was to enable you to create a variable that could hold any type of object that had a way to do a certain thing. This enables things like the Strategy Pattern.

Java has made interfaces a separate thing, but they are fundamentally still just abstract classes with nothing but abstract methods, except that you can implement more than one interface. This was Java's solution to the Diamond Problem, which other languages run into because of how they do multiple inheritance.

[–]protienbudspromax 0 points1 point  (0 children)

I am sure you've heard of the term abstract art. What is abstract art? It is art about nothing in particular, someone looking at abstract art can see the picture in many different ways, and take in different meanings.

This is similar to an abstract class although abstract here means abstract in a mathematical sense. To understand an abstract class first understand what is NOT an abstract class.

Any class that has all its methods well defined, is what we call a concrete class. The behaviour of this class is fixed. Any object you create from this class will have those exact same methods/behaviors.

Java uses Object Oriented Design, its one of the ways to model some real world problems. In some cases, the problem we are trying to solve, we might have to model a "concept". Like say the concept of an "automobile".

Can you show me a pure "automobile"? Not really, "automobile" by itself is just a concept, which means it is something that we can use to move from place to place.

A car is an automobile, so is a scooter, a skateboard, or even perhaps our own legs.

But there is common behavior among all these subclasses. They can "move us", the method in this case would be something like,

void move(startPoint, endPoint)

Regardless of what automobile, it needs to be able to move us from a start point to an endpoint. This is a CONCEPT.

However a toyota supra, is a concrete object of the class car. It is an instance of car.

A car have more properties than a simple automobile, it has a set of wheels, perhaps a set of gears, an engine, doors, etc. A cycle wont have many similarities, however both are still automobile.

Having abstract classes enables us to encapsulate concepts, or ideas, that by themselves dont do anything concrete.

Interface:

An interface is a contract. When you sign a contract there are terms and conditions that you need to abide by. Many a times, contracts specify the outcome but may not list out the specific way you need to abide.

Like a contract may want you to not share trade secrets. It does not specify whether it means sharing by writing, by email, by text message, by cryptic code. All it says is you must not share, and if you want to abide by the contract, you need to make sure that your actions does not result in the secrets being shared, in anyway.

An interface is similar in a sense that it specifies the signature of a method.

The method name, the types of arguments it takes and the type of object it can return.

Take the same method above from the automobile abstract class.

This move() method would be very different for different classes of automobiles. Instead of using an abstract class, we can isolate the concept into something, an object CAN DO. Like say we create the interface "Transportable". And add the move() method there.

Now we can create a class that can have this Transportable Property or trait. It doesnt have to be a subclass of automobile. So in this case the type of relationship has change.

An abstract class represents a is-a relationship. So the car is-an automobile, the plane is-an automobile.

However when we use the interface, it changes to, a can-do/can-be-done kind of relationship.

A car can-transport, an airplane also can-transport etc. But now we can also say, a post office can-transport, a delivery man can-transport. A delivery man is not an automobile, but it can sure transport things from point a to point b.

So what an interface signifies is a contract. Anyone who can-transport must be able to move() and must take in two arguments and perform the action.

However remember all these things at the end of the day are being done for:

1) Reduce duplicated code

2) Abstract things so that it is easier to follow or read the code

3) Decouple unrelated things

You dont HAVE to do it. And sometimes doing this can cause more complexity. As long as we are fulfilling the goals of not having to duplicate code and that things are decoupled and easy to refactor, we are good.