37-Simple-Java-Inheritance

37-Simple-Java-Inheritance - CS107 Spring 2007 Handout 37...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance Handout written by Julie Zelenski, updated by Jerry. Inheritance is a language property most gracefully supported by the object-oriented paradigm. It provides a unique form of code sharing by allowing you to take the implementation of any given class and build a new class that refines and extends the original. This new subclass starts by inheriting all of the data and operations defined in the superclass. It can then extend the behavior by adding additional data and new methods to operate on it. The subclass can also extend or replace behavior in the superclass by overriding methods that were already implemented. The inheritance hierarchy can be many levels deep. At the root of the entire class hierarchy is the Object class, which is the granddaddy of all Java classes. The Employee class The trivial Employee class tracks the hours worked by an employee along with their hourly wage and their "attitude" which gives you a rough measure of what percentage of time they are actually productive: /** * Class: Employee * --------------- * Employees have a "productivity" factor that represents how much * they produce -- accessed by getProductivity(). They also (accurately) * report their salary. */ public class Employee { public Employee(int wage, int hours, double att) // constructor { wagePerHour = wage; numHoursPerWeek = hours; attitude = att; } public double getProductivity() { return numHoursPerWeek * attitude; } public int getSalary() { return wagePerHour * numHoursPerWeek; } // instance variables protected double attitude; protected int numHoursPerWeek, wagePerHour; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Subclassing Employee "Subclassing" and "superclass" are the broadly accepted terms in the OOP world. The OOP subculture sometimes calls a superclass a "base" class and a subclass a "derived" class. Derive and subclass can both be used as verbs, but not in polite conversation. e.g. "So then thinking quickly, I just derived off of the window base class and I was golden." The following creates a subclass of Employee , the Boss class. The Boss is a more specialized version of Employee that adds an ArrayList of "underling" sub-employees. Note that Boss represents an isa relationship with Employee . A Boss is a particular type of Employee , so it has all the same properties plus a few more. Boss overrides the getProductivity method to add in the work done by the underlings and adds a few new methods of its own dealing with underlings. In addition, bosses have the annoying habit of lying about their salary, they always say they make twice as much as they really do. /** * Class: Boss * ----------- * Like an Employee, but also has a ArrayList of underling employees (which * may be empty). The getProductivity() of a Boss adds in the * productivity of any underlings. Also they lie when asked about * their salary. */ public class Boss extends Employee { // subclass of Employee
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

37-Simple-Java-Inheritance - CS107 Spring 2007 Handout 37...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online