In this post, we will learn the difference between abstract class and interface in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Features | Abstract Class | Interface |
---|---|---|
Multiple Inheritance | An abstract class doesn't support multiple inheritance. | An interface supports multiple inheritance. |
Default Implementation | An abstract class can have methods with default implementation. | Before Java 8, interfaces could not have methods with default implementations. From Java 8 onwards, interfaces can have default and static methods. |
Instance Variables | An abstract class can have instance variables. | An interface can have only static and final variables (constants). |
Constructors | An abstract class can have constructors. | An interface cannot have constructors. |
Type of methods | An abstract class can have abstract, non-abstract, static, final or non-final methods. | Before Java 8, an interface could only have abstract methods. From Java 8 onwards, interfaces can have default and static methods. |
Access Modifiers | Abstract classes can have public, protected and default methods. | Interface methods are implicitly public and abstract (if not static or default). |
Keyword | 'abstract' keyword is used to declare abstract class. | 'interface' keyword is used to declare interface. |
Sub Class | A class extending an abstract class has to provide implementation to its abstract methods. | A class implementing an interface has to provide implementation to all of its methods. |
Collection Framework
Java
X vs Y
Comments
Post a Comment