Concept of abstract class in very important in Java programming language.The key word to denote an abstract class is abstract . An abstract class can contain both abstract and concrete methods. An abstract class cannot be instantiated directly.So only subclass of the abstract class can be instantiated.If we specify a method in an abstract class as abstract method, then the sub classes should override the same method. If you are using IDE like eclipse , it will prompt you to do the implementation. The concrete methods need not override.
Abstract classes are useful when some functionality are common to all classes and some are different. Consider the simple example of an employee management system in a typical software service company. Consider there are two categories of employees only such as Manager and Developer.We have two methods in each classes. First method is to calculate the attendance and the second is to calculate salary. Calculation of attendance is same for both the types of employees. But calculation of salary is different , since the salary structure can be different. So we can define an abstract class Employee with a concrete method to calculate the attendance and an abstract method to calculate the salary.
public abstract class Employee {
public int calculateAttendance() {
return 30;
}
public abstract double calculateSalary();
}
In this case we are just returning attendance as 30 . But the salary calculation method is left as abstract so that the child classes can implement the salary computation depending on the employee category.Now let us inherit this abstract class .
public class Manager extends Employee {
@Override
public double calculateSalary() {
return 100000;
}
}
In case of manager the salary returning is 10000.Now let us see the code of developer
public class Developer extends Employee {
@Override
public double calculateSalary() {
return 50000;
}
}
In this case the salary will be 50000.Now let us see the main class to see the functionality.
public class EmployeeMain {
public static void main(String[] args) {
Employee emp = new Developer();
System.out.println("Developer : " + "Attendance =" + emp.calculateAttendance() + " Salary = " + emp.calculateSalary());
Employee emp1 = new Manager();
System.out.println("Manager : " + "Attendance =" + emp1.calculateAttendance() + " Salary = " + emp1.calculateSalary());
}
}
As of our understanding the attendance displayed should be same for both manager and developer,but the salary should be different.Now let us see the output of EmployeeMain.java
Developer : Attendance =30 Salary = 50000.0
Manager : Attendance =30 Salary = 100000.0
Abstract methods in Non abstract classes
Abstract methods cannot be written in non abstract classes.Abstract methods can be written only in abstract classes.
Difference between interface and Abstract class
An interface can be identified with keyword Interface Abstract class is like a normal class but an abstract keyword is there.Abstract can contain abstract as well as concrete methods.But interface can contain only abstract methods . In case of abstract class abstract methods needs to be specified with keyword ‘abstract’, but it is not needed in case of interface