Abstract class vs Interface in Java with example

Nikhiljain08 December 28, 2017
0 people like this post
abstract_class_vs_interface

Abstract class and interfaces are the perfect example of Abstraction property of object-oriented programming paradigm. Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

Abstract Class:

An Abstract is a class which is defined as by using keyword abstract in the declaration.

  • It contains methods which are defined as abstract or non-abstract and data members can be final, non-final, static, non-static.
  • If a class includes abstract methods, then the class itself must be declared abstract.
  • If the abstract class is inherited and then the subclass needs to provide implementation of all abstract methods present in its parent class.
  • It cannot be instantiated, but they can be inherited as Super Class.

 

Example:

Let’s start with declaring an abstract class, ShapeObject, which contains shape method and other two abstract methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways.

The ShapeObject class can look something like this:

 

Each non abstract subclass of ShapeObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

 

Output:

Shape function is called
Draw a circle
Resize the circle

Shape function is called
Draw a rectangle
Resize the rectangle

 

Interface:

An Interface is a reference type which contains methods and data members, similar to a class but without implementation details. Method bodies exist only for default methods and static methods. It contains only constants, method signatures, default methods, static methods, and nested types.

  • By default, all the members in interface are static, final and public.
  • We cannot instantiate an interface.
  • They can only be implemented by classes or extended by other interfaces.
  • Implements multiple inheritance, one of the OOPs property.

 

Example:

Let’s start with declaring an interface, Shape which contains two methods, such as draw or resize, that need to be implemented by all sub classes but must be implemented in different ways.

The Shape interface can look something like this:

Each class, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

 

Output:

Draw a circle
Resize the circle

Draw a rectangle
Resize the rectangle
 

Abstract Class vs Interface

Points Abstract Class Interface
Declaration Abstract keyword is used. Interface keyword is used.
Access Level Members can be static/non-static, final/non-final or abstract/non-abstract All members are static, final and abstract by default.
Inheritance Inherited using extends keyword Inherited using implements keyword
Multiple inheritance Doesn’t support multiple inheritance Supports multiple inheritance
Implementation It can provide implementation of interface It cannot provide implementation of abstract class.
Multiple implementation It can extend another Java class and implement multiple Java interfaces It can extend another Java interface only
Category: Java
  • 0
  • 241
Nikhiljain08