Java provides a powerful mechanism to achieve abstraction using the abstract
keyword. Abstraction is a process of hiding implementation details and exposing only the essential features of an object. This mechanism allows developers to define classes and methods without implementing them, ensuring a structure that subclasses must follow.
What is the ‘abstract’ Keyword?
The abstract
keyword can be applied to both classes and methods:
- Abstract Class: A class declared with the
abstract
keyword is known as an abstract class. It may or may not include abstract methods. - Abstract Method: A method that is declared without a body (no implementation) and is defined using the
abstract
keyword is called an abstract method.
Syntax:
1. Abstract Class:
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("Eating...");
}
}
Code language: JavaScript (javascript)
2. Abstract Method:
abstract void makeSound();
Code language: JavaScript (javascript)
Key Points About Abstract Classes:
- An abstract class cannot be instantiated directly.
- If a class contains even one abstract method, it must be declared as
abstract
. - Subclasses of an abstract class must provide implementations for all abstract methods or be declared abstract themselves.
- An abstract class can have constructors, instance variables, concrete methods, and static methods.
- It can have both abstract and non-abstract methods.
Example Program: Implementing Abstract Classes
Problem Statement: LotusJavaPrince and Mahesh want to design a system that manages different types of employees. Each employee type must have a unique way of calculating salary, but all employees must share common attributes like name and ID.
Solution: Use an abstract class to define the structure for employees and implement different salary calculation methods in subclasses.
// Abstract class Employee abstract class Employee { String name; int id; Employee(String name, int id) { this.name = name; this.id = id; } abstract double calculateSalary(); void displayDetails() { System.out.println("ID: " + id + ", Name: " + name); } } // Subclass PermanentEmployee class PermanentEmployee extends Employee { double basicSalary; PermanentEmployee(String name, int id, double basicSalary) { super(name, id); this.basicSalary = basicSalary; } @Override double calculateSalary() { return basicSalary + (0.2 * basicSalary); } } // Subclass ContractEmployee class ContractEmployee extends Employee { double hourlyRate; int hoursWorked; ContractEmployee(String name, int id, double hourlyRate, int hoursWorked) { super(name, id); this.hourlyRate = hourlyRate; this.hoursWorked = hoursWorked; } @Override double calculateSalary() { return hourlyRate * hoursWorked; } } // Main class public class Main { public static void main(String[] args) { Employee permEmp = new PermanentEmployee("LotusJavaPrince", 101, 50000); Employee contEmp = new ContractEmployee("Mahesh", 102, 20, 160); permEmp.displayDetails(); System.out.println("Salary: " + permEmp.calculateSalary()); contEmp.displayDetails(); System.out.println("Salary: " + contEmp.calculateSalary()); } } /* ID: 101, Name: LotusJavaPrince Salary: 60000.0 ID: 102, Name: Mahesh Salary: 3200.0 */
Why Use Abstract Classes?
- Abstract classes provide a way to define a common interface for all subclasses while allowing each subclass to implement its specific behavior.
- They help in maintaining the structure of the application, enforcing the necessary methods for subclasses.
- They prevent instantiation of incomplete classes and ensure that essential methods are implemented in derived classes.
The abstract
keyword and abstract classes play a vital role in Java for implementing abstraction. By defining abstract methods, we can enforce specific method structures in subclasses while allowing each subclass to implement its own logic. This concept is fundamental for creating scalable and maintainable applications in Java.