When to use the abstract class

Every programmer knows abstract class in java is used to achieve the abstraction in java. But only a few of them know when to use abstract class in java? You can’t use such things in your program if you don’t know when we should use them?

Suppose we want to maintain the data of a Company. A company has different types of employee in different departments like Software developer, IT, Management. Each employee has different personal information and salary.  So, we need to create a class Company and some subclasses that inherit the method of Company class.

Firstly, we will discuss the problem area. After that we will find the solution with abstract class.

when to use abstract class in java

Let’s say we have a class Company that has two methods companyDetail() and employeeDetail(). The companyDetail() method contains the information of the company that is common for all employees and employeeDetails() method contains the information of employees which differs for every employee. We have three subclasses for employees that are Developer, Manager and IT. Since each employee has different personal information and salary. So, every subclass overriding the employeeDetails() method to give the details of its own implementation. Here you must have noticed, the employeeDetails() method in Company class (Parent class) is not creating any value because every child class overriding the method.

class Company 
{	
	public void companyDetail()
	{
		System.out.println("Company name : Final Rope");
		System.out.println("Company GST : 123xxxxxxxxxx");
	}
	
	public void employeeDetail()
	{
		System.out.println("Employee name : Ram");
		System.out.println("Employee ID : 101");
		System.out.println("Employee Salary : XXXXX");
	}
}

class Developer extends Company
{
	public void employeeDetail()
	{
		System.out.println("Employee name : Sham");
		System.out.println("Employee ID : 201");
		System.out.println("Employee Salary : XXXXX");
	}
}

class Manager extends Company
{
	public void employeeDetail()
	{
		System.out.println("Employee name : Krishan");
		System.out.println("Employee ID : 301");
		System.out.println("Employee Salary : XXXXX");
	}
}

class IT extends Company
{
	public void employeeDetail()
	{
		System.out.println("Employee name : John");
		System.out.println("Employee ID : 401");
		System.out.println("Employee Salary : XXXXX");
	}
}

public class MainClass 
{
	public static void main(String arg[])
	{
		Developer developer = new Developer();
		developer.companyDetail();
		developer.employeeDetail();
		
		Manager manager = new Manager();
		manager.companyDetail();
		manager.employeeDetail();
		
		IT it = new IT();
		it.companyDetail();
		it.employeeDetail();
	}
	
}

Output: Company name : Final Rope
Company GST : 123xxxxxxxxxx
Employee name : Sham
Employee ID : 201
Employee Salary : XXXXX
Company name : Final Rope
Company GST : 123xxxxxxxxxx
Employee name : Krishan
Employee ID : 301
Employee Salary : XXXXX
Company name : Final Rope
Company GST : 123xxxxxxxxxx
Employee name : John
Employee ID : 401
Employee Salary : XXXXX

There is no point to implement the employeeDetails() method in Company class. Hence, we should make the employeeDetails() method as abstract method. By making this method abstract there is no need to give any implementation in parent class. Th abstract method(employeeDetails() method) force all the sub classes to implement this method. If any sub class will not provide the implementation to this abstract method, then it will show compilation error.

when to use abstract class in java
abstract class Company 
{	
	public void companyDetail()
	{
		System.out.println("Company name : Final Rope");
		System.out.println("Company GST : 123xxxxxxxxxx");
	}
	
	public abstract void employeeDetail();
	
}

class Developer extends Company
{
	public void employeeDetail()
	{
		System.out.println("Employee name : Sham");
		System.out.println("Employee ID : 201");
		System.out.println("Employee Salary : XXXXX");
	}
}

class Manager extends Company
{
	public void employeeDetail()
	{
		System.out.println("Employee name : Krishan");
		System.out.println("Employee ID : 301");
		System.out.println("Employee Salary : XXXXX");
	}
}

class IT extends Company
{
	public void employeeDetail()
	{
		System.out.println("Employee name : John");
		System.out.println("Employee ID : 401");
		System.out.println("Employee Salary : XXXXX");
	}
}

public class MainClass 
{
	public static void main(String arg[])
	{
		Developer developer = new Developer();
		developer.companyDetail();
		developer.employeeDetail();
		
		Manager manager = new Manager();
		manager.companyDetail();
		manager.employeeDetail();
		
		IT it = new IT();
		it.companyDetail();
		it.employeeDetail();
	}
}

Output: Company name : Final Rope
Company GST : 123xxxxxxxxxx
Employee name : Sham
Employee ID : 201
Employee Salary : XXXXX
Company name : Final Rope
Company GST : 123xxxxxxxxxx
Employee name : Krishan
Employee ID : 301
Employee Salary : XXXXX
Company name : Final Rope
Company GST : 123xxxxxxxxxx
Employee name : John
Employee ID : 401
Employee Salary : XXXXX

As we have seen above example in detail. Now we can predicate the scenarios and think in a better way and discuss when to use abstract class in java?

Leave a Comment