What is an abstract class? Difference between abstract class & interfaces.

What is an abstract class?

An abstract class defines an abstract concept which can’t be instantiated. We can’t create object of abstract class, it can only be inherited. Abstract class normally represents concept with general actions associated with it.

Explain the difference between abstract class and interfaces.

Abstract class can’t be instantiated, it can only be inherited while interfaces has to be implemented.
Abstract class can have implemented methods which interfaces can have only definitions of the methods without implementation.

Explain the difference between abstract class and interfaces with an example for each.

The differences between abstract class and interface are as follows:

Abstract Class

Interface

Can have abstract methods and concrete methodsCan have only method signatures and static final members
All methods are not by default publicAll methods by default abstract and public
An abstract class can extend another abstract classAn interface can extend another interface but not a class
The extended class can override the methods of its super class and its hierarchyAn implementing class can implement multiple interfaces
All abstract methods must have abstract access modifierAll methods in an interface must not have abstract access modifier

The following examples illustrate the differences:
abstract class Shape
{
   abstract void area();
   abstract void perimeter();
   void someMethod() // concrete method
   {
       ..
   }
}

interface Shape
{
   void area();
   void perimeter();
}
Java interface - What is an interface?
Java interface - An interface is a set of method definition without implementation. It is a protocol of...
JAVA JVM and JIT - Explain JVM (Java virtual machine) and JIT (Just-in-time compilation)
JAVA JVM and JIT - JVM is a virtual computer that runs the compiled java program. JVM is software that stays on the top...
How to implement polymorphism in JAVA.
Java polymorphism - Capacity of a method to do different things based on the object that it is...
Post your comment