Sunday, September 9, 2012

What is difference between interface and abstract class in Java

What is abstract class and interface in Java
Difference between abstract class and interface in Java is one of the tricky Java interview question and mostly appear in core Java interviews. Both interface and abstract class is a way to achieve abstraction in Java but there are significant difference between both of them. some time interviewer also not just focus on key differences between abstract class and interface in Java but also interested in When to use interface in Java and When to use abstract class is Java, This is a tricky pat of this interview question and  you must have good understanding of What is interface and abstract class in Java and How to use them. Anyway in this Java article we will first see some syntactical difference between interface and abstract class in Java programming language and later we will see where to use abstract class and interface
.

Abstract class vs interface in Java

In last section we saw what is abstract class and interface and now let's see difference between interface and abstract class in Java.

1) First and major difference between abstract class and interface is that, abstract class is a class while interface is a interface, means by extending abstract class you can not extend another class because Java does not support multiple inheritance but you can implement multiple inheritance in Java.

2) Second difference between interface and abstract class in Java is that you can not create non abstract method in interface, every method in interface is by default abstract, but you can create non abstract method in abstract class. Even a class which doesn't contain any abstract method can be abstract by using abstract keyword.

3) Third 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 cases but if you are writing a time critical application than you may not want to leave any stone unturned.

4) Fourth difference between abstract class vs interface in Java is that, interface are better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective.

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 abstract class you can provide default implementation in super class.


That's all on difference between abstract class and interface in Java, I will add more differences whenever I learn new things.

Other Java tutorials you may like

10 comments:

  1. Great post - very informative. I found this page to be very helpful as well:

    http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/

    ReplyDelete
  2. Thanks dude really helpful ....

    ReplyDelete
  3. i lerned very well by IT??????????

    ReplyDelete
  4. Thank u... It s very useful for me.

    ReplyDelete
  5. i don't understand where we use interface and where we use abstract class ?

    ReplyDelete
  6. 1. A class can implement more than one interface(Multiple Inheritance is possible with interface). But A class can not extend more than one abstract class(Multiple Inheritance is not possible with class).

    2. If You want to add a new method in interface, you need to implement that method in all the classes that implementing the interface. But if you want to add a new method in abstract class, you can add it as a non abstract method. You don't need to implement it in all the classes that extending the abstract class.

    3. Interface don't have constructors. But Abstract class has constructors.

    Prathap
    Java training in chennai

    ReplyDelete
  7. If we don't want to allow anybody to create the object to our class,then we should go for abstract class.
    Ex:HttpServlet.

    ReplyDelete
  8. This is all going to change in Java 1.8. the introduction of Functional interfaces changes the differences listed here :)

    ReplyDelete
  9. In an interface we cannot define static methods and can only define public static final
    variables

    ReplyDelete

Java67 Headline Animator