Java Abstract Class
In the concept of object-oriented programming, all objects are described through classes, but conversely, not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such a class is an abstract class.
An abstract class, except for not being able to instantiate objects, retains all other functionalities of a class, such as access to member variables, member methods, and constructors, which are the same as in a regular class.
Since an abstract class cannot instantiate objects, it must be inherited to be used. This is also the reason why the decision to design an abstract class is usually made during the design phase.
The parent class contains common methods for the collection of subclasses, but since the parent class itself is abstract, these methods cannot be used directly.
In Java, an abstract class represents an inheritance relationship, where a class can only inherit from one abstract class, but a class can implement multiple interfaces.
Abstract Class
In the Java language, an abstract class is defined using the abstract class
keyword. Here is an example:
Employee.java File Code:
/* File name : Employee.java */
public abstract class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name
+ " " + this.address);
}
public String toString()
{
return name + " " + address + " " + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}
Note that the Employee class does not differ significantly, despite being an abstract class. It still has 3 member variables, 7 member methods, and 1 constructor. Now, if you try the following example:
AbstractDemo.java File Code:
/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{
/* The following is not allowed and would cause an error */
Employee e = new Employee("George W.", "Houston, TX", 43);
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}
When you attempt to compile the AbstractDemo class, the following error will occur:
Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error
Inheriting an Abstract Class
We can inherit the attributes of the Employee class as follows:
Salary.java File Code:
/* File name : Salary.java */
public class Salary extends Employee
{
private double salary; // Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName() + " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
Although we cannot instantiate an object of the Employee class, if we instantiate an object of the Salary class, it will inherit 7 member methods from the Employee class, and through these methods, we can set or get three member variables.
AbstractDemo.java File Code:
/* File name : AbstractDemo.java */
public class AbstractDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}
The above program compiles and runs with the following output:
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.
Abstract Methods
If you want to design a class that contains a particular member method whose implementation is determined by its subclasses, you can declare the method as abstract in the parent class.
The Abstract keyword is also used to declare abstract methods, which contain only a method name and no method body.
An abstract method is not defined; the method name is followed directly by a semicolon, not curly braces.
public abstract class Employee {
private String name;
private String address;
private int number;
public abstract double computePay();
// Remaining code
}
Declaring an abstract method results in the following two outcomes:
If a class contains abstract methods, the class must be declared as abstract.
Any subclass must override the abstract methods of the parent class, or declare itself as abstract.
Subclasses that inherit abstract methods must override them. Otherwise, the subclass must also be declared as abstract. Ultimately, there must be a subclass that implements the abstract method; otherwise, neither the initial parent class nor the final subclass can be used to instantiate objects. If the Salary class inherits from the Employee class, it must implement the computePay() method: ## Salary.java File Code:
/* File name : Salary.java */ public class Salary extends Employee { private double salary; // Annual salary
public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; }
// Other code } ```
Summary of Abstract Class Rules
-
- Abstract classes cannot be instantiated (a common mistake for beginners), and attempting to do so will result in an error that prevents compilation. Only non-abstract subclasses of an abstract class can create objects.
-
- An abstract class does not have to contain abstract methods, but a class with abstract methods must be abstract.
-
- Abstract methods in an abstract class are only declared and do not include a method body, meaning they do not provide the specific implementation of the method.
-
- Constructors and class methods (methods decorated with the static keyword) cannot be declared as abstract methods.
-
- Subclasses of an abstract class must provide concrete implementations of the abstract methods, unless the subclass itself is also abstract.