May 10, 2021 Java
In object-oriented concepts, all objects are depicted by classes, but in turn, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.
In addition to abstract classes that cannot instantiate objects, other functions of the class still exist, and member variables, member methods, and construction methods are accessed in the same way as normal classes.
Because abstract classes cannot instantiate objects, abstract classes must be inherited to be used. It is also for this reason that it is often decided at the design stage whether to design abstract classes.
The parent class contains common methods for children's collections, but cannot be used because the parent class itself is abstract.
Use abstract class to define abstract classes in the Java language. Here's an 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, and although it is an abstract class, it still has three member variables, seven member methods, and one construction method. 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, 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
We can inherit the Employee class in a general way:
/* 文件名 : 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 an Employee class, if we instantiate a Salary class object, the object inherits three member variables and seven member methods from the Employee class.
/* 文件名 : 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.
If you want to design a class that contains a special member method whose specific implementation is determined by its children, you can declare the method an abstract method in the parent class.
Abstract keywords can also be used to declare abstract methods that contain only one method name, not a method body.
Abstract methods are not defined, and the method name is followed directly by a sign, not a parenthesis.
public abstract class Employee
{
private String name;
private String address;
private int number;
public abstract double computePay();
//其余代码
}
Declaring abstract methods results in two results:
Sub-classes that inherit abstract methods must override the method. O therwise, the sub-class must also be declared an abstract class. Ultimately, there must be sub-classes to implement this abstract method, otherwise neither the original parent class nor the final sub-class can be used to instantiate the object.
If the Salary class inherits the Employee class, it must implement the putePay() 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;
}
//其余代码
}