Latest web development tutorials

Java abstract classes

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

In addition to the abstract class can not be instantiated objects, the other functional classes still exist, the way to access the member variables, methods and constructors of the members and the same general category.

Since the abstract class can not be instantiated object, so the abstract class must be inherited, it can be used. For this reason also, usually determined at the design stage or not to design an abstract class.

Parent class contains a set of common subclass, but due to the parent class itself is abstract, you can not use these methods.


Abstract class

Use to define abstract class abstract class in the Java language. The following examples:

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

Noting that the Employee class is no different, although the class is an abstract class, but it still has three member variables, methods, and seven members of a 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 AbstractDemo class, it will produce the following error:

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

Abstract class inheritance

Our general approach inherited by 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;
   }
}

While we can not instantiate an object of class Employee, but if we instantiate a Salary class object that inherits from the Employee class variables and three members of the seven members of the 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 above program is compiled results 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 a class that contains a specific member method, the specific implementation of the method is determined by its subclasses, then you can declare the method in the parent class is an abstract method.

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

Abstract method is not defined, directly behind the method name with a semicolon instead of curly braces.

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

Declare abstract methods will result in the following two results:

  • If a class contains abstract methods, the class must be abstract.
  • Any subclass must override abstract methods of the parent class, or declare itself abstract.

Inherited abstract methods subclasses must override this method. Otherwise, the subclass must be declared as an abstract class. Ultimately, there must be subclasses to implement the abstract method, otherwise, from the initial to the final parent class subclass can not be used to instantiate the object.

If the Salary class inherits the Employee class, it must implement 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;
   }

   //其余代码
}