In the last section, we saw what is abstract class and interface and now let's see the difference between interface and abstract class in Java.
1) First and the major difference between abstract class and an interface is that an abstract class is a class while the interface is an interface, means by extending the abstract class you can not extend another class because Java does not support multiple inheritances but you can implement multiple inheritance in Java.
2) The second difference between interface and abstract class in Java is that you can not create a non-abstract method in an interface, every method in an interface is by default abstract, but you can create a non-abstract method in abstract class. Even a class which doesn't contain any abstract method can be made abstract by using the abstract keyword.
3) The third difference between abstract class vs interface in Java is that interface is better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective. The Effective Java has one item dedicated to explaining about why you should be using interface for type declaration. You should check that out as well.
4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java. This is not a significant difference in most of the cases but if you are writing a time critical application then you may not want to leave any stone unturned.
5) Another notable difference between interface and abstract class is that 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 an abstract class you can provide a default implementation for a new method in the superclass without breaking existing clients.
That's all on the difference between abstract class and interface in Java, I will add more differences whenever I learn new things. As I said, in the first paragraph, after the introduction of default method in Java 8 (See Java 8 in Action) and the provision that you can have both static and default method inside an interface, the difference between abstract class and interface has become blur. Earlier, the interface only contains contract no implementation but now they can.
Difference between Abstract Class and Interface in Java
Contrast between Abstract Class and Interface is one of the prominent inquiry question. Theoretical Class and Interface are center piece of java programming dialect. Regardless of whether to picked interface or unique class is a structure choice that each draftsman faces.In this post, we will find out about the distinction between dynamic class and Interfa...
Difference between interface and abstract class in Java
What is abstract class in JavaAn abstract class is something which is incomplete and you can not create an instance of the abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or i...
Difference between Abstract class vs Interface in Java
The question usually starts with a difference between abstract class and interface in Java, which is rather easy to answer, especially if you are familiar with the syntax of Java interface and abstract class. Things start getting difficult when interviewer ask about when to use abstract class and interface in Java, which is mostly based upon a solid understa...
Top Searches - Trending Searches - New Articles - Top Articles - Trending Articles - Featured Articles - Top Members
Developed by Interfuse - Copyright © 2010 - 2018 Uberant.com