Java Abstract Class Example

This Java example demonstrates how to create an abstract class and it's usage in Java with an example.

What is an Abstract Class?

An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
Abstract Class Definition Rules:
  1. Abstract classes cannot be instantiated directly.
  2. Abstract classes may be defined with any number, including zero, of abstract and non-abstract methods.
  3. Abstract classes may not be marked as private or final.
  4. An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
  5. The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.

Java Abstract Class Example

In this example, we create an abstract Employee class and which contains the abstractcalculateSalary () method. Let the subclasses extend Employee class and implementcalculateSalary() method.

Let's create Contractor and FullTimeEmployee classes as we know that the salary structure for a contractor and full-time employees are different so let these classes to override and implement a calculateSalary() method.

Let's write source code by referring the above class diagram.

Step 1: Let's first create the abstract superclass named an Employee. Let's define a method called calculateSalary() as an abstract method in this abstract Employee class. So the method is abstract and we can leave the implementation of this method to the inheritors of the Employee class.
public abstract class Employee {
 
    private String name;
    private int paymentPerHour;
 
    public Employee(String name, int paymentPerHour) {
         this.name = name;
         this.paymentPerHour = paymentPerHour;
    }
 
    public abstract int calculateSalary();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPaymentPerHour() {
       return paymentPerHour;
    }
    public void setPaymentPerHour(int paymentPerHour) {
        this.paymentPerHour = paymentPerHour;
    }
}
Step 2: The Contractor class inherits all properties from its parent Employee but has to provide its own implementation to calculateSalary() method. In this case, we multiply the value of payment per hour with given working hours.
public class Contractor extends Employee {
 
    private int workingHours;
    public Contractor(String name, int paymentPerHour, int workingHours) {
        super(name, paymentPerHour);
        this.workingHours = workingHours;
    }
    @Override
    public int calculateSalary() {
        return getPaymentPerHour() * workingHours;
    }
}
Step 3: The FullTimeEmployee also has its own implementation of calculateSalary() method. In this case, we just multiply by a constant value of 8 hours.
public class FullTimeEmployee extends Employee {
    public FullTimeEmployee(String name, int paymentPerHour) {
        super(name, paymentPerHour);
    }
    @Override
    public int calculateSalary() {
        return getPaymentPerHour() * 8;
    }
}
Step 4: Let's create a test class to test the Abstract class.
public class AbstractClassDemo {

    public static void main(String[] args) {

        Employee contractor = new Contractor("contractor", 10, 10);
        Employee fullTimeEmployee = new FullTimeEmployee("full time employee", 8);
        System.out.println(contractor.calculateSalary());
        System.out.println(fullTimeEmployee.calculateSalary());
    }
}
Read more about abstract classes at https://www.javaguides.net/2018/06/guide-to-create-abstract-class-with.html.

Related Java OOPS Examples


Comments