Thu Dec 06 2018

Interface vs Abstract Class in Java

Interface vs Abstract Class in Java

Abstract Class and Interface both are core part of Java programming language. Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated. However, they are very different from each other.

Whether to choose interface or abstract class is a design decision that every architect faces, but there are many differences between abstract class and interface that you should know before using them.

So, here we are going to highlight the differences between abstract class and Interface and clear the conclusion when should you use interface over abstract class and vice versa.

Let’s get started -

Abstraction class

An abstract class in Java is a class that is declared abstract - it may or may not contain abstract methods. It is quite similar to a Java interface except that fact that it can contain default method implementation. Like in object-oriented programming, abstraction in Java is achieved using abstract classes and interfaces. In Java, abstraction means hiding the irrelevant details from the user to focus only on the essential details to increase efficiency thereby reducing complexity. An abstract class may or may not contain abstraction methods (methods without body), but if a class has at least one abstract method, it must be declared abstract. The methods in an abstract class can have access modifiers such as private, public, static and protected, depending on the visibility level.

Interface

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. So it is kind of signing a contract, you agree that if you implement this interface, then you have to use its methods. It is just a pattern, it can not do anything itself. An interface is more of a blueprint of a class which is also used to achieve abstraction in Java. It contains abstract methods and static constants. In simple terms, an interface is a collection of abstract methods that are used to specify a behavior that the class has to implement. Unlike an abstract class, an interface provides full abstraction in Java. It can have both methods and variables just like a class, however, the methods declared in an interface are abstract by default.

Interface vs Abstract class

  • 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 cannot extend another class because Java does not support multiple inheritances but you can implement multiple inheritances in Java.

  • An abstract class can extend only one class or one abstract class at a time. But, an interface can extend any number of interfaces at a time.

  • "abstract" keyword is used to create an abstract class and it can be used with methods also whereas "interface" keyword is used to create an interface and it can’t be used with methods.

  • An interface is slow whereas an abstract class is fast.

  • An abstract class can have final, non-final, static and non-static variables. On the other side, an interface has only static and final variables.

  • An abstract class can provide the implementation of an interface. But, the interface can't provide the implementation of the abstract class.

  • An abstract class can extend another Java class and implement multiple Java interfaces. In contrast, an interface can extend another Java interface only.

  • An abstract class can be extended using the keyword "extends". An interface class can be implemented using keyword "implements".

  • A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.

  • While adding new stuff to the interface, it is a nightmare to find all the implementers and implement newly defined stuff. In the case of Abstract Class, you can take advantage of the default implementation.

  • An interface is abstract so that it can't provide any code. In contrast, an abstract class can give complete, default code which should be overridden.

  • Interfaces help to define the peripheral abilities of a class. On the other side, an abstract class defines the identity of a class.

  • An interface is better to use interface when various implementations share only method signature. Polymorphic hierarchy of value types. On the other hand, an abstract class should be used when various implementations of the same kind share a common behavior.

  • The interface cannot contain data fields. But, the class can have data fields.



 

You can share your experiences with us in the comment section. Thank you!

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.