1. Abstract and Final Classes in Java 8
1. Abstract Class
An abstract class in Java is a class that cannot be instantiated directly and may contain
abstract methods (methods without implementation). It is used as a base class for other
classes to provide a common structure.
Key Features of Abstract Class:
o Can have both abstract (without body) and concrete (with body) methods.
o Cannot be instantiated directly.
o Must be inherited by a subclass.
o Can have constructors, fields, and static methods.
Abstract Class Example: Employee & Salary Structure
Program: Abstract Class Demonstration
// Abstract class
abstract class Employee {
String name;
int empId;
// Constructor
Employee(String name, int empId) {
this.name = name;
this.empId = empId;
}
// Abstract method (must be implemented by subclasses)
abstract double calculateSalary();
// Concrete method
void displayEmployee() {
System.out.println("Employee ID: " + empId + ", Name: " +
name);
}
}
// Concrete subclass
class Salary extends Employee {
double basicSalary, bonus;
Salary(String name, int empId, double basicSalary, double bonus)
{
super(name, empId);
this.basicSalary = basicSalary;
2. this.bonus = bonus;
}
// Implementing abstract method
@Override
double calculateSalary() {
return basicSalary + bonus;
}
}
// Main class
public class AbstractClassExample {
public static void main(String[] args) {
// Creating an object of Salary (concrete class)
Salary emp1 = new Salary("John Doe", 101, 50000, 5000);
// Displaying details
emp1.displayEmployee();
System.out.println("Total Salary: " +
emp1.calculateSalary());
}
}
Step-by-Step Explanation of Abstract Class Example
1. Define an Abstract Class (Employee)
Has common attributes (name, empId).
Has an abstract method calculateSalary(), which will be implemented in subclasses.
Provides a concrete method displayEmployee() to show employee details.
2. Create a Subclass (Salary)
Extends Employee and provides an implementation for calculateSalary().
Adds basicSalary and bonus fields.
3. Create an Object of Subclass (Salary)
An object of Salary is created in main() as abstract classes cannot be instantiated.
Employee details are displayed using displayEmployee().
Salary is calculated using calculateSalary().
3. 2. Final Class
A final class in Java is a class that cannot be inherited. It is used when we want to restrict
modification of a class.
Key Features of Final Class:
o Cannot be subclassed (inherited).
o Can have concrete methods but no abstract methods.
o Ensures the security and integrity of critical classes.
Final Class Example: Employee & Salary Structure
Program: Final Class Demonstration
// Final class
final class Employee {
private String name;
private int empId;
private double basicSalary, bonus;
// Constructor
Employee(String name, int empId, double basicSalary, double
bonus) {
this.name = name;
this.empId = empId;
this.basicSalary = basicSalary;
this.bonus = bonus;
}
// Method to calculate salary
public double calculateSalary() {
return basicSalary + bonus;
}
// Display Employee details
public void displayEmployee() {
System.out.println("Employee ID: " + empId + ", Name: " +
name);
System.out.println("Total Salary: " + calculateSalary());
}
}
// Main class
public class FinalClassExample {
public static void main(String[] args) {
// Creating an object of the final class Employee
Employee emp1 = new Employee("Alice Brown", 202, 60000,
8000);
// Display Employee details
emp1.displayEmployee();
}
}
4. Step-by-Step Explanation of Final Class Example
1. Define a Final Class (Employee)
o Declared as final, so it cannot be extended.
o Contains private attributes (name, empId, basicSalary, bonus).
o Provides methods to calculate salary (calculateSalary()) and display
details (displayEmployee()).
2. Create an Object of Employee
o Since Employee is final, it can be used as is but cannot be inherited.
o The main() method creates an object and calls displayEmployee() to show
details.
Advantages and Disadvantages
Advantages of Abstract Class
Encapsulation & Code Reusability: Common features are defined in a base
class, reducing redundancy.
Flexible Implementation: Allows mix of concrete and abstract methods.
Polymorphism: Supports method overriding in subclasses.
Disadvantages of Abstract Class
Limited Multiple Inheritance: Java does not support multiple inheritance with
classes (only interfaces).
Performance Overhead: Requires subclassing, which may increase complexity.
Advantages of Final Class
o Security & Integrity: Prevents modification by subclassing.
o Thread Safety: Ensures consistency in multi-threaded applications.
o Optimization: The compiler can optimize final classes better.
Disadvantages of Final Class
o No Flexibility: Cannot be extended, limiting reuse.
o Not Suitable for Large Systems: Makes code less adaptable for changes.
Key Differences between Abstract and Final Class
Feature Abstract Class Final Class
Purpose Acts as a base for subclasses Prevents further extension
Instantiation Cannot be instantiated Can be instantiated
Methods Can have abstract & concrete methods Only concrete methods
Inheritance Must be inherited Cannot be inherited
Use Case Used for polymorphism & extensibility Used for security & stability
Use an abstract class when you want a base class with common functionality and enforce
method implementation in child classes.
Use a final class when you want to prevent inheritance and ensure a class remains
unchanged.