In: Accounting
Similarities And Difference Between Abstract Class And Interface
Similarities
Differences
Differences between interface and abstract class in java software development language are as bellow.
Interface |
Abstract Class |
We can use interface keyword to declare interface. | We can use abstract keyword to declare abstract class. |
Interface can hold only abstract methods(without implementation). | Abstract class can hold abstract(without implementation) as well as non abstract methods. |
Interface can be implemented using implements keyword. | Abstract class can be extended using extends keyword. |
We can achieve multiple inheritance using interface as we can implement multiple interfaces to any class. | Abstract class doesn't support multiple inheritance as we can not extend more than one class. |
Interface can not hold main method, static methods or constructor. | Abstract class can hold main method, static methods or constructor. |
Also it can hold only static and final variables and mandatory to initialize them. | It can hold static, non static, final, non final variables and also it is not mandatory to initialize them. |
We can achieve 100% abstraction using interface as all methods are abstract by default. It can not hold concrete methods. | We can achieve partial(0% to 100%) abstraction using abstract class as it can hold abstract as well concrete methods too. |
When you add a new method in existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good. | By using abstract class you can provide default implementation in super class by creating concrete method. It is not required to provide its implementation in sub class. |
Default Modifiers of Abstract Methods and Variables:-
Default Modifiers of Interface Methods and Variables:-
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
Allowed modifiers of Abstract methods and Variables:-
Abstract class can have final, non-final, static and non-static variables.
An abstract method can only set a visibility modifier, one of public or protected.
Allowed modifiers of Interface methods and Variables:-
Only public and abstract modifiers are allowed for methods in an interfaces.
All variables declared inside interface are implicitly public static final variables(constants).