Online Tutorials & Training Materials | STechies.com
Register Login

Difference between Abstract Class and Interface

|| || 0

Difference between Abstract Class and Interface
Stechies

Before we begin with the differences between an abstract class and interface in java, it is important to understand the meaning of the term abstraction. Abstraction pertains to concealing the internal implementation specific features of objects and showcasing just the functionality to users. Abstract class and interface are both used for the purpose of abstraction. An important and frequently-asked interview question, the difference between interface and abstract class forms a core component of java programming language. So, as a Java programmer, if you have been wondering whether to choose abstract class or interface, then what follows is for you.

Abstract class in Java

Abstract class in Java serves to be similar to interface with the exception being that it can be used for containing default method implementation.

  • Abstract classes may contain the abstract method without having a body; also, they can contain methods along with implementation.
  • The ‘abstract’ keyword is used for the creation of an abstract class and/or method.
  • It is not possible to initiate an abstract class in Java.
  • The abstract class is generally used for providing a base for a subclasses to implement and extend and implement abstract methods. It is also used to override/use the implemented methods in any given abstract class.
  • If a class contains abstract methods, then it has to be defined as abstract by using the ‘abstract’ keyword; otherwise, it will fail to compile.
  • It is optional to have an abstract class for a class to contain the abstract method. A class is capable of being marked as abstract even if it fails to declare abstract methods.
  • As Java is non-supportive of multiple class inheritance, it is preferable to utilize interface in case there is no method implementation in the abstract class.
  • In Java codes, the subclass of any abstract class has to implement all abstract methods unless the specific subclass also happens to be an abstract class.
  • Abstract classes in Java are capable of being run like all other classes if they possess the main() method.
  • Java Abstract class provides common method implementations for subclasses so as to provide default implementation.
  • Java abstract classes can implement interfaces even without the provision / implementation of interface methods.

Interface in java

Interface is a reference type, which is similar to class in Java. It contains a group of abstract methods. An interface and the abstract methods contained by the interface are inherited by Java classes. In addition to the abstract methods, java interface may also comprise of default methods, constants, static methods as well as nested types.

  • The method bodies are present only for static methods and default methods.
  • The coding of an interface is akin to the writing of java classes. However, while a class depicts the behaviour and attributes of objects, the interface comprises of the behaviour implemented by a class.
  • In case the class implementing the interface is not abstract, the methods contained by the interface have to be defined in the class.
  • The ‘interface’ keyword is used for the declaration of an interface. 
  • An interface is abstract by default. Therefore, the abstract keyword need not be used while declaring an interface.
  • All methods contained in an interface are implicitly abstract, thus it is not necessary to use the abstract keyword.
  • Methods contained in java interface are implicitly public.
  • An interface can be made to extend in another interface in a manner similar to a class that is extendible into another class. To extend an interface, programmers in java use the ‘extends’ keyword. Once the interface is extended the child interface will inherit the methods depicted by the parent interface.

Abstract Class vs. Interface

The differences between abstract class and interface are as follows:

S.No.

Abstract Class

 Interface

1

 At any given point of time, an abstract class is capable of extending only one class or a singular abstract class.

 An interface is designed to extend multiple interfaces at any given point of time.

2

  Abstract classes are capable of extending other concrete (regular) classes and/ or abstract classes.

 Interfaces are capable of extending other interfaces only.

3

 An abstract class comprises of both  concrete and abstract methods.

Only abstract methods are present in an interface.

4

 The keyword “abstract” is used for declaring any method as an abstract method. It is mandatory.

 The keyword “abstract” is not mandatory for the purpose of declaring abstract methods.

5

 An abstract class may comprise of both protected and public abstract methods.

Interfaces in java comprise of public abstract methods only.

6

 Abstract classes are capable of having final, static, final and/or static final variables with the use of access specifiers.

 Only public static final variables are possible with interfaces in java.

7

The ‘abstract’ keyword is used for defining an abstract class and method alike.

The ‘interface’ keyword is used for defining an interface only. It cannot be used for method declaration.

8.

The ‘extends’ keyword is used by a subclass for extending an abstract class. Implementation of all declared methods present in the abstract class have to be provided in case the subclass is not an abstract class.

The ‘implements’ keyword is used by a subclass for the implementation of an interface. All the methods present in the interface have to be provided with proper implementation for the code to compile.

9.

Abstract classes may have methods with implementation.

 

Java interfaces offer absolute abstraction. It is not possible to have method implementations in them.

10.

Abstract classes possess constructors.

Interfaces do not possess constructors.

11.

Abstract class contain the features of any normal java class but these features cannot be instantiated.

Interfaces comprise of a different type and comprise of public static final method declarations and constants.

12.

Abstract class can be made to run if they contain the main() method.

 An interface cannot be made to run as it does not contain the main method implementation.

13.

 

An abstract class also defines contract along with providing other method implementations for usage by subclasses.

Interface in java is used for defining contract for subclasses.

14

Abstract classes may comprise of non-final variables.

Variables declared in any Java interface are by default final.

15

Abstract classes are faster than interfaces in Java.

Java interfaces are slower than abstract classes as they require extra indirection.


Related Articles

0.0112 seconds.