1. Classes, superclasses, and subclasses
2. Objects: the cosmic superclass
3. Generic Array lists
4. Objects wrappers and autoboxing
5. Methods with a variable number of parameters
6. Enumeration classes
7. Reflection
8. Design hints for inheritance
The idea behind inheritance is that you can create new classes that are built on existing classes. When you inherit from an existing class, you reuse
(Or inherit) its methods, and you can add new methods and fields to adapt your new class to new situations.
1. Classes, superclasses, and subclasses
Every manager is an employee
Here is how you define a manager class that inherits from the employee class
Class manager extends employee {Add methods and fields}
The keywordExtendsIndicates that you are makingNew ClassThat derives fromExisting class.
TheExisting classIs calledSuperclass, Base class, or parent class.
TheNew ClassIs calledSubclass, Derived class, or child class.
When defining a subclass by extending its superclass, you only need to indicate the differences between the subclass and the superclass. When
Designing classes, you place the most general methods into the superclass and more specialized methods in its subclasses. Factoring out common
Functionality by moving it to a superclass is common in object-oriented programming.
However, some of the superclass methods are not appropriate for the manager subclass. In particle, the getsalary method shocould return the sum
Of the base salary and bonus. You need to supply a new methodOverrideThe superclass method:
Public double getsalary () {return salary + bonus; // won't work}
However, that won't work. The getsalary method of the manager classHas no direct access to the private fields of the superclass.
Public double getsalary () {double basesalary = super. getsalary (); Return basesalary + bonus ;}
We need to indicate that we want to call the getsalary method of the employee super-class, not the current class. You use the special keyword
Super for this purpose. The callSuper. getsalary ()Callthe getsalary method of the employee class
Finally, let us supply a constructor
1 public Manager(String n, double s, int year, int month, int day)2 {3 super(n, s, year, month, day); 4 bonus = 0;5 }
Here, the keyword super has a different meaning. The instructionSuper (N, S, year, month, day );Is shorthand for "call the constructor of
Employee superclass with N, S, year, month, and day as parameters ."
The call using super must be the first statement in the constructor for the subclass.
P194
1 //Employee.java 2 import java.util.*; 3 4 public class ManagerTest{ 5 public static void main(String[] args) { 6 7 Manager boss = new Manager("Boss",50000,1987,12,1); 8 boss.setBonus(5000); 9 10 Employee[] staff = new Employee[3];11 staff[0] = boss;12 staff[1] = new Employee("lisi",30000,1989,10,1);13 staff[2] = new Employee("wangwu",20000,1990,3,12);14 15 // print out information about all Employee objects16 for (Employee e : staff)17 System.out.println("name=" + e.getName() + 18 ",salary=" + e.getSalary() + 19 ",hireDay=" + e.getHireDay());20 }21 }
1 //Manager.java 2 public class Manager extends Employee{ 3 4 private double bonus; 5 6 public Manager(String n, double s, int year, int month, int day){ 7 super(n, s, year, month, day); 8 bonus = 0; 9 }10 11 public double getSalary()12 {13 double baseSalary = super.getSalary();14 return baseSalary + bonus;15 }16 17 public void setBonus(double b)18 {19 bonus = b;20 }21 }
1 //ManagerTest.java 2 3 import java.util.*; 4 5 public class ManagerTest{ 6 public static void main(String[] args) { 7 8 Manager boss = new Manager("Boss",50000,1987,12,1); 9 boss.setBonus(5000);10 11 Employee[] staff = new Employee[3];12 staff[0] = boss;13 staff[1] = new Employee("lisi",30000,1989,10,1);14 staff[2] = new Employee("wangwu",20000,1990,3,12);15 16 // print out information about all Employee objects17 for (Employee e : staff)18 System.out.println("name=" + e.getName() + 19 ",salary=" + e.getSalary() + 20 ",hireDay=" + e.getHireDay());21 }22 }
Chapter 5 inheritance