Inheritance_edit
Inheritance_edit
What is Inheritance?
• Dog and Cat have the name field and the getName
method in common
●
Classes often have a lot of state and behavior in
common
●
Result: lots of duplicate code!
Solution: Inheritance
●
Inheritance allows you to write new classes
that inherit from existing classes
●
The existing class whose properties are
inherited is called the "parent" or
superclass
●
The new class that inherits from the super
class is called the "child" or subclass
●
Result: Lots of code reuse!
Dog Cat
String name String name
int fleas int hairballs
String getName() String getName()
int getFleas() int getHairballs()
void speak() void speak()
using
inheritance
superclass
Animal
subclass
String name
subclass String getName()
Dog Cat
int fleas int hairballs
int getFleas() int getHairballs()
void speak() void speak()
Animal Superclass
public Animal(String n) {
name = n;
}
●
What is the output of the following?
●
The subclass inherits all the fields and
methods of the superclass
●
The first thing a subclass constructor must do
is call the superclass constructor
●
This ensures that the superclass part of the
object is constructed before the subclass part
●
If you do not call the superclass constructor
with the super keyword, and the superclass
has a constructor with no arguments, then
that superclass constructor will be called
implicitly.
Implicit Super Constructor Call
public class A {
public A() { System.out.println("I'm A"); }
}
●
When you override a method, you can
call the superclass's copy of the method
by using the syntax super.method()
●
Variables have the types they are given
when they are declared and objects have
the type of their class.
●
For an object to be assigned to a variable
is must be of the same class or a
subclass of the type of the variable.
●
You may not call a method on a variable
if it's type does not have that method,
even if the object it references has the
method.
Which Lines Don't Compile?
●
"Casting" means "promising" the compiler
that the object will be of a particular type
●
You can cast a variable to the type of the
object that it references to use that
object's methods without the compiler
complaining.
●
The cast will fail if the variable doesn't
reference an object of that type.
Which Castings Will Fail?
●
A Company has a list of Employees. It asks you
to provide a payroll sheet for all employees.
– Has extensive data (name, department, pay amount, …) for
all employees.
– Different types of employees – manager, engineer,
software engineer.
– You have an old Employee class but need to add very
different data and methods for managers and
engineers.
●
Suppose someone wrote a name system, and already
provided a legacy Employee class. The old Employee class
had a printData() method for each Employee that only printed
the name. We want to reuse it, and print pay info.
// Constructor
public Employee(String fName, String lName) {
firstName= fName; lastName= lName;
}
// Method
public void printData() {
System.out.println(firstName + " " + lastName);}
}
Inheritance
Already written:
Class Employee
firstName printData()
lastName
is-a is-a
Class Engineer
Class Manager
firstName firstName
lastName lastName
hoursWorked
salary wages
printData()
getPay()
printData()
getPay()
You next write:
Engineer class
Subclass or (directly) derived class
class Engineer extends Employee {
private double wage;
private double hoursWorked;
public Engineer(String fName, String lName,
double rate, double hours) {
super(fName, lName);
wage = rate;
hoursWorked = hours;
}
Class Manager
firstName
lastName
Salary
is-a
printData
getPay
Class SalesManager
firstName
lastName
Salary
printData
getPay salesBonus
SalesManager Class
(Derived class from derived class)
class SalesManager extends Manager {
private double bonus; // Bonus Possible as commission.
Fred Smith
Weekly pay: $96.0
Ann Brown
Monthly salary: $1500.0
Mary Barrett
Monthly salary: $1250.0
Bonus: $2000.0
●
All Java classes implicitly inherit from
java.lang.Object
●
So every class you write will automatically have
methods in Object such as equals, hashCode, and
toString.
●
We'll learn about the importance of some of these
methods in later lectures.