Abstract class and interface have similarities. However both are quite different in behavior and usages. This made me to writing this article. I presume, you have the basic knowledge of Abstract class and Interface with respect to Java.
Myth: Many suggested that there is performance related difference between these two. Abstract class are better than interfaces in terms of performance. If JVM has JIT, then there is no performance related difference between these two. Check out more on Android Performance Tips
- Fundamental difference between these two with respect to Object Oriented Programming concepts lies in relationships. Interface represents a weak "is-a" (is a kind of) relationship, indicates that object possesses a particular property for example String class implements the Comparable interface; whereas abstract class represents a strong "is-a" relationship that clearly describes parent child relationship for example a Customer is a person.
- In interface, by default all members (variables) are constants aka public static final where as in Abstract Class there is no restriction with respect to member visibility.
In Interface, by default all methods are public abstract whereas in Abstract Class there is no restrictions.
Note: Java 8 onwards it is changed after the introduction of default method. Also, static methods are also got introduced in interfaces like Comparator.comparing(...).- Interface does not contain any constructor whereas though abstract classes also can not be instantiated by new operator, constructor are invoked by subclasses through constructor chaining.
- Multiple interfaces can be implemented at once which is not the case with Abstract Class as Java does not support multiple inheritance.
Interface only provides the method signatures; whereas Abstract Classes may provide the concrete implementation of the methods.
Note: This is applicable version 7 and below. Java now allows default and static methods in interfaces that have the concrete implementation.- Interface does not allow implementation class to modify the visibility of the methods; where as implementation of abstract class may have less or same restrictive visibility for abstract methods.
Myth: Many suggested that there is performance related difference between these two. Abstract class are better than interfaces in terms of performance. If JVM has JIT, then there is no performance related difference between these two. Check out more on Android Performance Tips
2 comments
Write commentsGreat explanation.The difference is, In abstract class you can define the method and in subclass you should override it.But incase of interface you can't define the method you can only declare and you should override it in subclass.
Replysankar
java course in chennai
Great Explanation.The difference is, In abstract class you can define the method and in subclass you should override it.But incase of interface you can't define the method you can only declare and you should override it in subclass.
Replyprathap
java course in chennai
Emoticon Emoticon