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 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 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 particular, the getSalary method should return the sum
of the base salary and bonus. You need to supply a new method to override the superclass method:
public double getSalary() { return salary + bonus; // won‘t work }
However, that won’t work. The getSalary method of the Manager class has 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 call super.getSalary() calls the 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 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.”
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 objects 16 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 objects 17 for (Employee e : staff) 18 System.out.println("name=" + e.getName() + 19 ",salary=" + e.getSalary() + 20 ",hireDay=" + e.getHireDay()); 21 } 22 }