CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept of this lecture is inheritance. This will require us to study the Java keywords extends, this, and super. The idea of inheritance will be motivated by extending class Employee of the previous lecture to distinguish between kinds of employees: VIPs (the CEO and other major officers), salaried employees, and regular employees. Each kind of employee requires different information in their record, so there is different processing for each. Placing the information for all three kinds of employees in class Employee wastes space and causes complications in the code; using classes that are extensions of class Employee eliminates these problems.
CS100A, Fall Lecture 5 2 CS100A, Fall 1997, 11 September Concepts for this lecture: Inheritance: a subclass B of a class A inherits fields and methods from class A. A is a super-class of B. In Java, keyword extends is used to in defining a subclass. Using the constructor of a super class. Access modifier protected Overriding methods Method toString() for every class Reading: Lewis/Loftus,
CS100A, Fall Lecture 5 3 Method toString // Yield a String containing the data for // the person public String toString() {return name + " " + salary + " " + hireDate; } Convention: Every class implements method toString! In … + B (catenation), if B is not a String, method B.toString() is used to convert it to a String.
CS100A, Fall Lecture 5 4 // An instance of Employee contains a person's name, // salary, and year hired. It has a constructor and // methods for raising the salary, printing the data, and // retrieving the person's name and the year hired. public class Employee {public String name; // The person's name public double salary;// The person's salary public int hireDate;// The year hired // Constructor: a person with name n, salary s, and // year d hired public Employee(String n, double s, int d) {name= n; salary= s; hireDate= d; } Our old class Employee
CS100A, Fall Lecture 5 5 // Raise the salary by p percent public void raiseSalary(double p) {salary= salary * (1 + p/100.0);} // Yield the year the person was hired public int hireYear() {return hireDate;} // Yield the person's name public String getName() {return name;} // Yield a String containing the data for the person public String toString() {return name + " " + salary + " " + hireDate; } (old class Employee)
CS100A, Fall Lecture 5 6 Modify our record of employees to take into account three different kinds: VIPS (e.g. CEO, president, vice president): Need a field bonus, since VIPS get (big) bonuses. Get a yearly wage. Salaried: Expected to work overtime whenever necessary, without extra pay. Regular: Time cards! Have an hourly wage instead of a yearly salary. (Need also to record the number of hours worked on each day, but we’ll forego that here and assume 40 hours per week. Our task today
CS100A, Fall Lecture 5 7 How to implement? Add fields: int employeeKind; // 1 = VIP, 2 = salaried, // 3 = regular double bonus; // bonus for VIP (only) double hourlyWage; // (for regular // employees only // Set bonus to b (if employee is a VIP) public setBonus(double b) {if (employeeKind = 1) bonus= b; } A lot of other changes are required.
CS100A, Fall Lecture 5 8 Problems with this approach. Each employee’s record has a field for a bonus and for an hourly wage, even though it may not need it. Within the methods of class Employee that deal with salary and wages and hours, tests will have to be made to determine which kind of employee is being processed. This makes the code longer, less well-structured, and confusing. If a new kind of employee were to be added that required a different processing for pay, all the code that deals with salary would have to be changed. Instead, use a different approach: inheritance.
CS100A, Fall Lecture 5 9 // An instance of Employee contains a person's name, // yearly pay, and year hired. public class Employee {protected String name; // The person's name protected double pay;// The person's yearly pay protected int hireDate;// The year hired // Constructor: a person with name n and year d hired public Employee(String n, int d) {name= n; salary= 0; hireDate= d;} // Yield the year the person was hired public int hireYear() {return hireDate;} // Yield the person's name public String getName() {return name;} // Yield a String containing the data for the person public String toString() {return name + " " + pay + " " + hireDate;} }
CS100A, Fall Lecture 5 10 Employee x; x= new Employee(“Gries”, 1969); x Employee Name Gries pay 0 hireDate 1969 Employee, hireYear, getName, toString
CS100A, Fall Lecture 5 11 // An instance of VIP contains a VIP's data public class VIP extends Employee {private double bonus; // The VIP's bonus // Constructor: a person with name n, year d hired, // yearly pay s, and bonus b public VIP(String n, int d, double s, double b) {super (n,d); pay= s; bonus= b;} // Yield a String containing the data for the person public String toString() {return “VIP + " name + " " + pay + " " + " " + bonus + " " + hireDate; } // Raise the salary by p percent public void raiseSalary(double p) {pay= pay * (1 + p/100.0);} }
CS100A, Fall Lecture 5 12 An instance of class VIP has every field and method that an instance of class Employee does, plus the ones VIP declares. Employee x; x= new Employee(“Gries”, 1969); Employee y= new VIP(“Perkins”,1983, 90000,1000); Employee Name Gries pay 0 hireDate 1969 Employee, hireYear, getName, toString x VIP Name Perkins pay hireDate 1983 Employee, hireYear, getName, toString bonus 1000 VIP raiseSalary y
CS100A, Fall Lecture 5 13 Use of super For an instance of VIP. A constructor of super class Employee should be called to initialize the fields declared within Employee. This is as the first statement in a constructor for VIP: super (n,d); Use of protected If fields of Employee are public, they can be referenced from anywhere. If they are private, they can be referenced only from instances of Employee. If they are protected, they can be referenced only from Employee and its extensions (subclasses).
CS100A, Fall Lecture 5 14 Referencing methods VIP y; y= new VIP(“Perkins”, 1983, 90000, 1000); y.getName() refers to method getName of its superclass, Employee. This is because getName is not defined in VIP. y.raiseSalary() refers to method raiseSalary of VIP. This is because raiseSalary is defined in VIP. y.toString() refers to method toString of VIP. This is because toString is defined in VIP. Method toString of superclass Employee has been overridden in class VIP.