Lecture 4
Lecture 4
Programming II
Lecture 4: Inheritance
The software crisis
• software engineering: The practice of developing, designing,
documenting, testing large computer programs.
• code reuse: The practice of writing program code once and using it
in many contexts.
Law firm employee analogy
• common rules: hours, vacation, benefits, regulations ...
– all employees attend a common orientation to learn general
company rules
– each employee receives a 20-page manual of common rules
– Example:
public class Secretary extends Employee {
...
}
– Are we finished?
– This will require us to modify our Employee class and add some new
state and behavior.
– Example:
public class Lawyer extends Employee {
public Lawyer(int years) {
super(years); // calls Employee constructor
}
...
}
Same Class √ √ √ √
Same Package, √ √ √ X
subclass
Different Package, √ √ X X
subclass
Different Package, √ X X X
Not subclass
37
Another Example
//********************************************************************
// Book Class (parent class)
// Represents a book. Used as the parent of a derived class to
// demonstrate inheritance and the use of the super reference.
//********************************************************************
//----------------------------------------------------------------
// Constructor: Sets up the book with the specified number of
// pages.
//----------------------------------------------------------------
public Book(int numPages)
{
pages = numPages;
}
continue
continue
//----------------------------------------------------------------
// Pages mutator.
//----------------------------------------------------------------
public void setPages(int numPages)
{
pages = numPages;
}
//----------------------------------------------------------------
// Pages accessor.
//----------------------------------------------------------------
public int getPages()
{
return pages;
}
}
//********************************************************************
// Dictionary class (child class)
// Represents a dictionary, which is a book. Used to demonstrate
// the use of the super reference.
//********************************************************************
//-----------------------------------------------------------------
// Constructor: Sets up the dictionary with the specified number
// of pages and definitions.
//-----------------------------------------------------------------
public Dictionary(int numPages, int numDefinitions)
{
super(numPages);
definitions = numDefinitions;
}
continue
continue
//-----------------------------------------------------------------
// Prints a message using both local and inherited values.
//-----------------------------------------------------------------
public double computeRatio()
{
return (double) definitions/pages;
}
//----------------------------------------------------------------
// Definitions mutator.
//----------------------------------------------------------------
public void setDefinitions(int numDefinitions)
{
definitions = numDefinitions;
}
//----------------------------------------------------------------
// Definitions accessor.
//----------------------------------------------------------------
public int getDefinitions()
{
return definitions;
}
}
//********************************************************************
// Words.java
//
// Demonstrates the use of the super reference.
//********************************************************************