Inheritance is the key of object-oriented programming. It is the act of sharing state and behavior from a more general type of class (superclass). It allows code to be shared between classes (software re usability). Saves time when programming, encourages reuse of well designed objects, helps keep code simple.
Is a class that does the inheriting by using state (instance variables) and behavior (methods) from some other class. It cannot access private methods or instance variables from its superclasses.
Is a class from which its subclasses inherit state and behavior. It is more general, than its subclasses.
Is the superclass from which the subclass explicitly inherits. Example: mammal is the direct superclass of dog.
class Animal {…} //superclass
class Mammal extends Animal {…} //subclass Mammal of superclass Animal
class Reptile extends Animal {…} //etc...
class Whale extends Mammal {…}
class Dog extends Mammal {…}
class Snake extends Reptile {…}
class Lizard extends Reptile {…}
class Iguana extends Lizard {…}
class Skink extends Lizard {…}
class Animal {
public int age;
public getAge(){ return age};
}
class Mammal extends Animal {
public float bodyTemperature;
public get bodyTemperature{return bodyTemperature}
class Dog extends Mammal {
String name;
}
When a message (method) is sent to an instance, Java must look up the method in the class hierarchy. If Java does not find the method defined in the class of the receiver, it checks the superclass.
If not found their either, it checks the superclass of that class and so on … until the Object class is reached. This lookup occurs at runtime (i.e. when the program is executing).
For class methods, the look-up occurs at compile time.
Since an inherited class method is compiled as static (i.e., lookup at compile time) then within that method the object must be treated as an instance of the class that defines the method, not the subclass that inherits it.
Is the term given to a class that inherits from more than one direct superclass. It is not supported in Java (but it is in C++).
Is a class without instances, always has subclasses, helps to organize a program.
Is a class that can have instances.
Is used when a method belonging to a superclass is not needed or is inappropriate. Allows the re-writing of the method for use in the subclass only. The subclass own method with the same name (and same parameter list) that does something else (perhaps nothing).
Person (name, phone#, address)
Employee (employee#, workPhone#)
Professor (courses, office #)
Secretary (workload, schedule)
Student (student#, courses, gpa)
FullTimeStudent (major)
PartTimeStudent (workPhone#)
public class Person {
protected String name, phoneNumber, address;
/* Place code for methods here */
}
public class Employee extends Person {
protected int employeeNumber;
protected String workPhoneNumber;
/* Place code for methods here */
}
public class Professor extends Employee {
protected Vector courses;
protected String officeNumber;
/* Place code for methods here */
}
public class Secretary extends Employee {
protected Vector workLoad;
protected Hashtable schedule;
/* Place code for methods here */
}
public class Student extends Person {
protected int studentNumber;
protected Vector courses;
protected float gpa;
/* Place code for methods here */
}
public class FullTimeStudent extends Student {
protected String major;
/* Place code for methods here */
}
public class PartTimeStudent extends Student {
protected String workPhoneNumber;
/* Place code for methods here */
}
Which class gets what? Which classes should implement getName or getcourses methods? What should the toString methods return?
Possible toString output:
Person NAME: Jim Class ADDRESS: 1445 Porter St. PHONE #: 845-3232 Employee NAME: Rob Banks ADDRESS: 789 ScotiaBank Road. PHONE #: 899-2332 EMPLOYEE #: 88765 WORK #: 555-2433 Professor NAME: Guy Smart EMPLOYEE #: 65445 WORK #: 232-3415 OFFICE #: 5240 PA Secretary NAME: Earl E. Bird ADDRESS: 12 Knowhere Cres. PHONE #: 443-7854 EMPLOYEE #: 76845 WORK #: 444-3243 Student NAME: May I. Passplease ADDRESS: 5567 Java Drive PHONE #: 732-8923 STUDENT #: 156753
toString for the Person class
//This is the toString method for the Person class
public String toString() {
return("Person \n" +
" NAME: " + getName() + "\n" +
" ADDRESS: " + getAddress() + "\n" +
" PHONE #: " + getPhoneNumer());
}
This method will be inherited! A better toString for the Person class:
//This is the toString method for the Person class
public String toString() {
return(this.getClass().getName() + "\n" +
" NAME: " + getName() + "\n" +
" ADDRESS: " + getAddress() + "\n" +
" PHONE #: " + getPhoneNumer());
}
toString for the Employee class
//This is the toString method for the Employee class
public String toString() {
return(super.toString() + "\n" +
" EMPLOYEE #: " + getEmployeeNumber() + "\n" +
" WORK #: " + getWorkNumer());
}
toString for the Professor class
//This is the toString method for the Professor class
public String toString() {
return(this.getClass().getName() + "\n" +
" NAME: " + getName() + "\n" +
" EMPLOYEE #: " + getEmployeeNumber() + "\n" +
" WORK #: " + getWorkNumer());
" OFFICE# : " + getOfficeNumber() + "\n");
}
toString for the Student class
//This is the toString method for the Student class
public String toString() {
return(super.toString() + "\n" +
" STUDENT# : " + getStudentNumber() + "\n");
}
It inherits from Person and also displays the student number.
A subclass automatically inherits the constructor of its superclass. Good programming style: override the superclass constructor. Should explicitly call the superclass constructor at the beginning of code. If not called explicitly, a superclass constructor is automatically called at the beginning of the subclass constructor.
Person constructors
public class Person {
protected String name, phoneNumber, address;
public Person() {
setName("");
setPhoneNumber("");
setAddress("")
}
public Person(String aName, String aPhoneNumber, String anAddress) {
setName(aName);
setPhoneNumber(aPhoneNumber);
setAddress(anAddress);
}
}
Employee Constructors
public class Employee extends Person {
protected int employeeNumber;
protected String workPhoneNumber;
public Employee() {
//There is an implicit call to the Person() constructor here
setEmployeeNumber(0);
setWorkPhoneNumber("");
}
public Employee(int aNumber, String aPhoneNumber) {
//There is an implicit call to the Person() constructor here
setEmployeeNumber(aNumber);
setWorkPhoneNumber(aPhoneNumber);
}
}
Professor Constructors
public class Professor extends Employee {
protected Vector courses;
protected String officeNumber;
public Professor() {
super();
courses = new Vector();
setOfficeNumber("");
}
public Professor(int aNumber, String aPhoneNumber, String anOfficeNumber) {
super(aNumber, aPhoneNumber);
courses = new Vector();
setOfficeNumber(anOfficeNumber);
}
}
An object of subclass can be treated as an object of its corresponding superclass. Professor can be treated as employee.