Classes, Superclasses, and Subclasses
class Manager extends Employee
{
added methods and fields
}
The keyword extends indicates that you are making a new class that derives from an existing class. The existing class is called the superclass, base class, or parent class. The new class is called the subclass, derived class, or child class.
When designing classes, you place the most general methods into the superclass and more specialized methods in the subclass. Factoring out common functionality by moving it to a superclass is common in object-oriented programming.
key word - super
public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
}Finally, let us supply a constructor.
public Manager(String n, double s, int year, int month, int day){ super(n, s, year, month, day); bonus = 0;}
Here, the keyword super has a different meaning. The instruction
super(n, s, year, month, day);
is shorthand for "call the constructor of the Employee superclass with n, s, year, month, and day as parameters."
Because the Manager constructor cannot access the private fields of the Employee class, it must initialize them through a constructor.
The constructor is invoked with the special super syntax. The call using super must be the first statement in the constructor for the subclass.
The fact that an object variable (such as the variable e) can refer to multiple actual types is called polymorphism.
Automatically selecting the appropriate method at run time is called dynamic binding.

浙公网安备 33010602011771号