javase personal garbage review notes 12 Java abstract class

Abstract class
Use abstract class to define abstract class in Java language (abstract class cannot be concreted).
Example:

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

Error:

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

Inheriting the abstract class
We can inherit the Employee class in the usual way:

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, the object will inherit 7 member methods from the Employee class, and three member variables can be set or obtained through this 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();
    }
}
/*以上程序编译运行结果如下:

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, the class contains a special member method, 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

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

Declaring abstract methods will cause the following two results:

If a class contains abstract methods, then the class must be an abstract class.
Any subclass must override the abstract method of the parent class, or declare itself as an abstract class.

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;
   }
 
   //其余代码
}
  1. Abstract classes cannot be instantiated (a mistake that beginners can easily make). If they are instantiated, an error will be reported and the compilation will fail. Only non-abstract subclasses of abstract classes can create objects.

  2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.

  3. The abstract method in the abstract class is just a declaration, without the method body, that is, the specific implementation of the method is not given, that is, the specific function of the method.

  4. Construction methods, class methods (methods modified with static) cannot be declared as abstract methods.

  5. A subclass of an abstract class must give concrete implementations of the abstract methods in the abstract class, unless the subclass is also an abstract class.

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108607902