i

JAVA Complete Course

Abstract classes vs Interfaces

 

We have already mentioned few times that abstraction is process of  showing only the functionality to the users and hiding the internal implementation of the it. i.e. what it works (showing), how it works (hiding). Both abstract class and interface are used for abstraction.

Let’s visit the differences between interfaces and abstract classes as they have couple of them:

  • Type of methods: Interface has only abstract methods. Abstract class has ability to have abstract and non-abstract methods together. From Java 8, interfaces can have default and static methods also.

  • Final Variables: By default variables in interface are final. An abstract class can have non-final variables.

  • Type of variables: Abstract class has possibility to have non-final, final, non-static and static variables. Interface has only static and final variables.

  • Implementation: Interface can’t provide the implementation of abstract class.  Abstract class has ability to provide the implementation of interface.

  • Inheritance vs Abstraction: Interface can be implemented using keyword “implements” while abstract class can be extended using keyword “extends”.

  • Multiple implementations: An interface can extend more than one interfaces, while  an abstract class can extend only one Java class and implement multiple interfaces.

  • Accessibility of Data Members: Interface members are public while in Abstract class we can have private, protected and public  members.