Friday, 16 February 2018

Difference between interface and abstract class in Java



Difference between interface and abstract class in Java
The difference between abstract class and interface in Java is one of the tricky Java interview question and mostly appear in core Java interviews. It has become now even trickier after Java 8 introduced default methods and allowed interfaces to have both default and static methods.

What is abstract class in Java

An 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 interface it has implemented or extended. By the way Java has a concept of abstract classes, abstract method but a variable can not be abstract in Java.
                      
                              
      Even though both interface and abstract class is a way to achieve abstraction in Java, there are significant differences between them, which you will learn in this article. Some time interviewer also not just focus on key differences between abstract class and interface in Java but he is also interested in some practical experience e.g. when to use interface in Java and when to use abstract class is Java. This is actually the tricky part of this interview question and  you must have a good understanding of what is an 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.

Though, if you are preparing for Java programming interview, you should also check out the Java Programming Interview Exposed, a great book specially designed to prepare for Java concepts based interview questions. It has many such questions from all important topics e.g. oop concepts, multithreading, collections, frameworks like Spring and Hibernate, unit testing, data structure and algorithm, coding, design pattern and modern technology questions like Android, Scala, and other JVM languages.


Abstract class vs Interface in Java

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.







No comments:

Post a Comment