interfaceVSabstractClass
1. Interface:
Like a class, an Interface can have methods and variables, but the methods declared in interface are by default abstract(only method signature, no body).
Interfaces specify what a class must do and not how. It is the blueprint of the class.
An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract.
A Java library example is, Comparator Interface. If a class implements this interface, then it can be used to sort a collection.
To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in interface are declared with empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword.
1.1 New features added in interfaces in JDK 8
Prior to JDK 8, interface could not define implementation. We can now add default implementation for interface methods. This default implementation has special use and does not affect the intention behind interfaces.
Suppose we need to add a new function in an existing interface. Obviously the old code will not work as the classes have not implemented those new functions. So with the help of default implementation, we will give a default body for the newly added functions. Then the old codes will still work.
e.g.
Another feature that was added in JDK 8 is that we can now define static methods in interfaces which can be called independently without an object. Note: these methods are not inherited.
1.2 Important points about interface or summary of article:
We can't create instance (interface can't be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class.
A class can implement more than one interface.
An interface can extends another interface(but only one interface).
All the methods are public and abstract. And all the fields are public, static, and final.
It is used to achieve multiple inheritance.
It is used to achieve loose coupling.
1.3 New features added in interfaces in JDK 9
From Java 9 onward, interfaces can contain following also:
Static methods
Private methods
Private Static methods
2. Abstract Classes in Java
In Java, a separate keyword abstract is used to make a class abstract. It can have abstract methods(methods without body) as well as concrete methods(regular methods with body).
2.1 Following are some important observations about abstract classes in Java
Like C++, in Java, an instance of an abstract class cannot be created, we can have references of abstract class type though.
Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of a inherited class is created.
e.g.
3. interface VS abstract class
Type of methods:
Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
Final Variables:
Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
Type of variables:
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Implementation:
Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
Inheritance vs Abstraction:
A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
Multiple implementation:
An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
Accessibility of Data Members:
Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
4. When to use what?
Consider using abstract classes if any of these statements apply to your situation:
In java application, there are some related classes that need to share some lines of code then you can put these lines of code within abstract class and this abstract class should be extended by all these related classes.
You can define non-static or non-final field(s) in abstract class, so that via a method you can access and modify the state of Object to which they belong.
You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
Consider using interfaces if any of these statements apply to your situation:
It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface.
A class can implement more than one interface. It is called multiple inheritance.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
Last updated
Was this helpful?