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 methods |
Can have only method signatures and static final members |
All methods are not by default public | All methods by default abstract and public |
An abstract class can extend another abstract class | An interface can extend another interface but not a class |
The extended class can override the methods of its super class and its hierarchy | An implementing class can implement multiple interfaces |
All abstract methods must have abstract access modifier | All 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();
}