Abstract class and Interface are used to implement abstraction in Java. In this post we are focusing on Java 7 interface and abstract class differences.
Default and static methods concept introduced in Java8. You can learn about default and static methods in interface in changes in interface in Java 8.
Default and static methods concept introduced in Java8. You can learn about default and static methods in interface in changes in interface in Java 8.
Interfaces always provides 100% abstraction, while abstract class can be used to get abstraction level from 0 to 100%. Although interface and abstract class are both used to implement abstraction, these both have many differences.
Below are the main differences between Interface and Abstract class.
- Interface always provides 100% abstraction, while abstract can provide 0 to 100% abstraction.
- Interface can have only abstract methods, but abstract class can have abstract and non-abstract methods.
- Interface can have only static and final variables (constants), but in abstract we can declare final, non-final, static and non-static (constant and non-constant both) variables.
- Multiple inheritance is allowed in interfaces in Java, but multiple inheritance on classes is not allowed. (Multiple inheritance of classes creates diamond problem in Java).
- Interfaces can only have abstract methods, but abstract classes can have static methods, constructors and main methods also.
- Interface are declared with interface keyword, while abstract classes are declared with abstract keyword.
- Interfaces are a little bit slower than abstract classes, as they require some extra indirections. But with latest Java versions and JIT these difference are negligible.
These are the basic differences between Interface and Abstract class. If you have any doubts please comment, I will try to resolve ASAP.
Check below links also-
Difference between abstraction and encapsulation
Difference between abstraction and encapsulation
***********************Keep studying***********************
No comments:
Post a Comment