Abstract Class in Java
A class definition prefixed with the abstract keyword is called an abstract class.
An abstract class may have both abstract methods and concrete methods.
Level of Abstraction
| Type | Level of Abstraction |
|---|---|
| Regular Class | 0% abstraction |
| Abstract Class | 0% to 100% abstraction |
| Interface | 100% abstraction |
Why Use an Abstract Class?
An abstract class is useful to create a class with partial implementation.
Some methods can contain common implementation, while other methods can be left for subclasses to implement.
Example 1: Abstract Class
abstract class Animal
{
abstract void eat();
void sleep()
{
System.out.println("I can sleep");
}
}
Any animal can sleep and it is common to all animals. But each animal may eat differently. Therefore, sleep() is a concrete method, while eat() is an abstract method.
Example 2: Abstract and Concrete Methods
abstract class Sample
{
int x;
abstract void test(); // abstract method
void show() // concrete method
{
}
}
An abstract method does not have a method body. A concrete method has a method body and contains implementation.
Constructor in an Abstract Class
An abstract class can have a constructor, but it cannot be instantiated directly.
The constructor of an abstract class is executed when an object of its subclass is created.
Example: Constructor in Abstract Class
abstract class Sample
{
int x;
Sample()
{
x = 15;
}
abstract void test();
void show()
{
System.out.println("x=" + x);
}
}
Creating Object of Abstract Class
public class AbstractEg
{
public static void main(String[] args)
{
Sample obj = new Sample(); // Error
}
}
An abstract class cannot be instantiated directly. Therefore:
Sample obj = new Sample();is not allowed.
Extending an Abstract Class
If you are extending an abstract class that has an abstract method, you must either:
- Provide the implementation of the abstract method.
- Make the new class abstract.
abstract class Animal
{
abstract void eat();
void sleep()
{
System.out.println("I can sleep");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog eats bones");
}
}
Here, the Dog class provides the implementation of the abstract eat() method.
Interface vs Abstract Class vs Regular Class
| Type | When to Use |
|---|---|
| Interface | When you want to define a capability or role. |
| Abstract Class | When you want to share code and enforce some method implementation. |
| Regular Class | When you don't need abstraction. |
Easy Real-Life Analogy
It tells the worker what he/she should be able to do.
Abstract Class = Partially Trained Worker
The worker knows some tasks but must learn other tasks.
Regular Class = Fully Trained Worker
The worker is completely ready to work.
Quick Revision
- Abstract class is declared using the abstract keyword.
- It may contain abstract as well as concrete methods.
- An abstract class cannot be instantiated directly.
- An abstract class can have a constructor.
- The constructor executes when a subclass object is created.
- A subclass must implement inherited abstract methods or itself be declared abstract.
- Abstract classes are useful for partial implementation.
Practice Questions
Write a Java program with an interface Shape having method area().
Create an abstract class Polygon that implements Shape and has a concrete method display().
Create a class Rectangle that extends Polygon and implements area().
Write a Java program where interface Bank has method getRateOfInterest().
Implement it in classes SBI, ICICI, and AXIS.
In main(), create objects and display interest rates.
Write a Java program where interface Vehicle has:
- Abstract method start()
- Default method stop()
- Static method service()
Implement Vehicle in class Car and demonstrate calling all methods.
Write a Java program where interface Playable has method play().
Implement it in classes Guitar and Piano.
In main(), use an interface reference to call play() on both objects.
Write a Java program where interface Flyable has method fly().
Interface Swimmable has method swim().
Class Duck implements both interfaces.
In main(), create a Duck object and call both methods.
Challenge Question
Create an abstract class Employee containing:
- A variable name
- A constructor to initialize the name
- An abstract method calculateSalary()
- A concrete method displayName()
Create two subclasses:
- FullTimeEmployee
- PartTimeEmployee
Implement calculateSalary() differently in both classes and demonstrate runtime polymorphism using an Employee reference.