Week 28 — What is the difference between abstract classes and interfaces in Java?
Question of the Week #28
What is the difference between abstract classes and interfaces in Java?
8 Replies
Classes contain actual behavior and state while interfaces typically just contain information what classes implementing the interface can do.
Interfaces can be seen as a contract or specification. By implementing an interface, the class must provide all methods specified in the interface.
On the other hand, abstract classes are typically incomplete classes. They can contain state and behaviour but some implementation could be missing. As with interfaces, concrete (non-abstract) classes need to provide an implementation of all abstract methods from parent classes:
Java does not allow a class to extend multiple other classes. As such, it is not possible to create a class extending two abstract classes.
However, any class can implement as many interfaces as it likes:
⭐ Submission from dan1st#0000
Interface cannot have method implementations but abstract classes can have methods with implementation
Abstract class can have public and private both, but interface just public
You can make a class inherit an other class but not the interface
Submission from thehsb#0000
The difference between an abstract class and an interface is that you can implement multiple interfaces but you can only extend one superclass. Interfaces allow you to define behavior for classes or objects that are different. Abstract classes are better for when you have objects that are similar
Submission from Chowder#5643
The difference between an abstract class and an interface is how in an interface, everything is final. Also they are called differently,
One uses extend, which can inly be used once per class, and one uses implements, which can be used multiple times.
An interface is more like a contract, where you put methods in that you require the user to define, while in an abstract class you can define them and the classes can inherit that method.
Output:
Submission from mochatitan#0000
Most obviously, the difference between an abstract class and an interface is functionally in that an implementing class can only “extend” one class, whereas it can “implement” any number of interfaces. Also, an abstract class can define and use instance variables whereas an interface cannot. Moreover, an abstract class can extend other abstract classes as well as implement any number of interfaces, whereas an interface can only extend other interfaces.
More importantly though, their purpose is also different. An abstract class is used to define “what” the the object is, whereas an interface defines the object’s “traits”.
For instance, a class “Cat” may extend an abstract class “Animal” and implement the interface “Tamable” which might define the method “tame()”.
In this case, we could say “Cat is an animal, which has the trait of being tamable”.
Although you might consider refactoring that so that there is instead an abstract class Pet which implements Tamable and extends Animal, and then Cat can extend Pet. It’s important to structure your inheritance with reusability in mind, since that’s kind of the whole point.
Submission from justisr#0000
Abstract classes in Java allow you to create classes that can contain both regular and abstract methods. When a class extends an abstract class, it must implement the abstract methods defined in the abstract class. Abstract methods are declared without a body and are meant to be overridden and implemented by the subclasses.
On the other hand, interfaces in Java are used to define a contract that a class can implement. Interfaces only contain abstract method declarations, and you don’t need to use the ‘abstract’ keyword when declaring these methods. A class that wants to use the functionality defined in an interface must implement the interface using the ‘implements’ keyword.
Submission from xgpq#0000
Interfaces are more of a building plan for a class. Interfaces can't declare instance fields, constructors, they can only define methods. Most of the time, those methods won't have an implementation in the interface itself, but interfaces can provide their own,
default
implementation with the aforementioned modifier on the method. Interfaces are often used to abstract away some class members, and make them accessible from the interface itself, to help with OOP.
Abstract classes are a bit more advanced. They're regular classes, can do everything a normal class can, but they can define abstract
methods, that don't have an implementation on their own. Similarly to an interface, child classes have to provide their own implementation of those abstract methods, or mark themselves abstract as well. If you want to instantiate an abstract class, you have to implement the abstract members in an anonymous inner class, same with interfaces.Interface-style implementation:
Abstract classes:
⭐ Submission from 0x150#0000