Java abstract class



In the object-oriented concept, all objects are described by classes, but conversely, not all classes are used to describe objects. If a class does not contain A class that contains enough information to describe a specific object is an abstract class.

Except that abstract classes cannot instantiate objects, other functions of the class still exist. Member variables, member methods and constructors can be accessed in the same way as ordinary classes.

Since 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 stage whether to design an abstract class.

The parent class contains common methods of the subclass collection, but because the parent class itself is abstract, these methods cannot be used.


Abstract class

Use abstract class in Java language to define abstract classes. The following example:

/* 文件名 : 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 */
public class AbstractDemo
{
   public static void main(String [] args)
   {
      /* 以下是不允许的,会引发错误 */
      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, the following error will be generated:

Employee.java:46: Employee is abstract; cannot be instantiated
      Employee e = new Employee("George W.", "Houston, TX", 43);
                   ^
1 error

Inheriting abstract class

We can pass The general method is to inherit the Employee class:

/* 文件名 : 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 Employee class object, if we instantiate a Salary class object, the object will inherit 3 member variables and 7 members from the Employee class method.

/* 文件名 : 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 results of compiling and running the above program are 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 such a class, which contains a special member method, the The specific implementation of the method is determined by its subclass, then you can declare the method as an abstract method in the parent class.

The Abstract keyword can also be used to declare abstract methods. Abstract methods only contain a method name and no method body.

The abstract method is not defined, and the method name is directly followed by a semicolon instead of curly braces.

public abstract class Employee
{
   private String name;
   private String address;
   private int number;
   
   public abstract double computePay();
   
   //其余代码
}

Declaring an abstract method will cause the following two results:

  •           If a class contains abstract methods, then the class must be abstract.

  •             Any subclass must override the abstract method of the parent class or declare itself as an abstract class.

Subclasses that inherit an abstract method must override this method. Otherwise, the subclass must also be declared abstract. Eventually, a subclass must implement the abstract method, otherwise, neither the initial parent class nor the final subclass can be used to instantiate the object.

If the Salary class inherits the Employee class, then it must implement the computePay() method:

/* 文件名 : 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;
   }

   //其余代码
}