Java abstract class

In the concept of object-oriented, all objects are described by classes, but in turn, not all classes are used to describe the object, if a class does not contain enough information to describe a specific object , Such a class is an abstract class.
In addition to the abstract class can not instantiate the object, the other functions of the class still exist, member variables, member methods and constructors are accessed in the same manner as the ordinary class.
Because abstract classes cannot instantiate objects, abstract classes must be inherited before they can be used. For this reason, it is usually decided during the design phase whether or not to design abstract classes.
The parent class contains common methods for subclass collections, but because the parent class itself is abstract, you cannot use these methods.
In Java, an abstract class represents an inheritance relationship. A class can inherit only one abstract class, and a class can implement multiple interfaces.

Abstract class

Abstract classes are defined using the abstract class in the Java language. The following 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; } }
Notice that the Employee class is no different. Although the class is 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, it will throw an error */ Employee e = new Employee("George W.", "Houston, TX", 43); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); } }
When you try to compile the AbstractDemo class, you get the following error:
Employee . java : 46 : Employee is abstract ; cannot be instantiated
       Employee e = new Employee ( "George W." , "Houston, TX" , 43 ); ^ 1 error       
                   

Inheritance abstract class

We can inherit the Employee class in a generic way:

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 a Salary class object, the object will inherit 7 member methods from the Employee class, and through this method you 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 as follows:
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 method

If you want to design a class that contains a special member method whose concrete implementation is determined by its subclasses, you can declare the method as an abstract method in the parent class.
The Abstract keyword can also be used to declare an abstract method. The abstract method only contains a method name and no method body.
The abstract method is not defined. The method name is followed by a semicolon instead of a curly bracket.
public abstract class Employee { private String name; private String address; private int number; public abstract double computePay(); //rest code }
Declaring an abstract method results in the following two results:
  • If a class contains an abstract method, the class must be an abstract class.
  • Any subclass must override the parent's abstract method, or declare itself as an abstract class.
Subclasses that inherit an abstract method must override this method. Otherwise, the subclass must also be declared as an abstract class. Ultimately, there must be a subclass that implements this abstract method; otherwise, neither the original parent class nor the final child class can be used to instantiate the object.
If the Salary class inherits the Employee class, then 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; } //rest code }


Abstract class summary

  • 1. Abstract class can not be instantiated (beginners can easily make mistakes), if it is instantiated, it will be an error, the compiler can not pass. Only non-abstract subclasses of abstract classes can create objects.
  • 2. Abstract classes do not necessarily include abstract methods, but classes with abstract methods must be abstract classes.
  • 3. The abstract method in an abstract class is just a declaration. It does not contain a method body, that is, it does not give a concrete realization of the method that is the specific function of the method.
  • 4. Constructors, class methods (methods decorated with static) cannot be declared as abstract methods.
  • 5. Subclasses of abstract classes must give concrete implementations of abstract methods in abstract classes, unless the subclass is also an abstract class.

Comments