在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
抽象类除了不能实例化对象之外,类的其它功能依然存在,成员变量、成员方法和构造方法的访问方式和普通类一样。
由于抽象类不能实例化对象,所以抽象类必须被继承,才能被使用。也是因为这个原因,通常在设计阶段决定要不要设计抽象类。
父类包含了子类集合的常见的方法,但是由于父类本身是抽象的,所以不能使用这些方法。
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;
}
} public class AbstractClass
{
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();
}
}
如上面代码,抽象类不能直接实例化对象,不然会发生编译错误!
使用抽象类
通过一般的方法继承Employee类:
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;
}
} 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);
System.out.println("Constructing a 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;
}
} public class AbstractClass
{
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();
}
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbsAAADHCAIAAAAPnGpHAAATbUlEQVR4nO2dQZLrNhJEedM529yEF5ill15544UjfIVZzHyZDVQlEiAogtJ7CwcFFasSBSBbit9Nb3///fdff/31559//vHHH7///vtvv/327//8a9u2fd+3A8XLR7Pv+/rTmdX//RenFQHAtt3rmFef5Oc6xUv5lCk8tw8AaxE65tsOGI6ZgWMCrMiAY+4HjiNb8h1w/4kzGF4XCcMSuqhW2KXf6Y+ZXycRk6r7kyXPBosS5kvRn+a8AJ5Nr2PWp+V1UZxtHR++rFM5ebLrLH847uh3HEEI60rVnHhxETZNlGumPTkO8JlMdExnXGfzB8/kD8ed/F12oC3yCscU+f35ZqVxTIBtu94xN/mtbX3H3Hq+Sh/j6ySOsGbAjY5ZoMcBPpPefys/+VnDCVvKMXstIHPJk46ZGeXrYqJjbtUHZCEYi4TvwnFM5+SPOWn2cabWWQTMkte88VXO/AB1rPhmxxQuP90x/cYCfBSZY275t9HaQcTL/Se1gPBGXXeL/MjJn5XQ+otsdYmsYt2EptpQZCasTl6nDfPUb9UCtCozD8CnIRwTtud8hporbNlpAtwMjtlk/c9QE+WtPE2A+8ExAQBccEwAABccEwDABccEAHDBMQEAXJzfx/ywfz9dbToDfV5KP8AXwTPY76W3z5/3AwzgSfAM9nsZ6/P68wL4THgG+72MKVx/XgCfCc9gH9Pv9KcZ+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