Programming using Java
Java: Inheritance
Objectives:
Get an introduction to Inheritance
Get a general idea of how a hierarchy of
classes is put together
Inheritance: Definition
inheritance: a parent-child relationship
between classes
allows sharing of the behavior of the
parent class into its child classes
– one of the major benefits of object-oriented
programming (OOP) is this code sharing
between classes through inheritance
child class can add new behavior or
override existing behavior from parent
3
Inheritance terms
superclass, base class, parent class:
terms to describe the parent in the
relationship, which shares its
functionality
subclass, derived class, child class:
terms to describe the child in the
relationship, which accepts functionality
from its parent
extend, inherit, derive: become a
subclass of another class
4
Inheritance in Java
in Java, you specify another class as
your parent by using the keyword
extends
– public class Rectangle
extends Shape {
– public class CheckingAccount
extends BankAccount {
Java forces a class to have exactly one
parent ("single inheritance")
– other languages (C++) allow multiple
inheritance
5
Inheritance in Java
– the objects of your class will now receive all of the
state (fields) and behavior (methods) of the parent
class
– constructors and static methods/fields are not
inherited
– by default, a class's parent is Object
6
Inheritance Example
class BankAccount {
private double myBal;
public BankAccount() { myBal = 0; }
public double getBalance() { return myBal; }
}
class CheckingAccount extends BankAccount {
private double myInterest;
public CheckingAccount(double interest) { }
public double getInterest() { return
myInterest; }
public void applyInterest() { }
}
CheckingAccount objects have myBal and
myInterest fields, and getBalance(), getInterest(), and
applyInterest() methods 7
Multiple layers of inheritance
it is possible to extend a class that itself is a child
class; inheritance chains like this can be arbitrarily
deep
public class TransactionFeeCheckingAccount
extends CheckingAccount {
private static final double FEE = 2.00;
public void chargeFee() {
withdraw(FEE);
}
}
8
"Has-a" Relationships
"Has-a" relationship: when one object
contains another as a field
public class BankAccountManager {
private List myAccounts;
// ...
}
a BankAccountManager object "has-a"
List inside it, and therefore can use it
9
"Is-a" relationships
"Is-a" relationships represent sets of abilities;
implemented through interfaces and inheritance
public class CheckingAccount
extends BankAccount {
// ...
}
a CheckingAccount object "is-a" BankAccount
– therefore, it can do anything an BankAccount can do
– it can be substituted wherever a BankAccount is needed
– a variable of type BankAccount may refer to a
CheckingAccount object
10
Using the account classes
CheckingAccount inherits BankAccount's
methods
CheckingAccount c = new
CheckingAccount(0.10);
[Link]([Link]());
[Link]();
);
11
Using the account classes
a BankAccount variable can refer to a
CheckingAccount object
BankAccount b2 = new CheckingAccount(0.06);
[Link]([Link]());
an Object variable can point to either account
type
Object o = new BankAccount();
Object o2 = new CheckingAccount(0.09);
12
Some code that won't compile
CheckingAccount variable can't refer to BankAccount
(not every BankAccount "is-a" CheckingAccount)
CheckingAccount c = new BankAccount();
cannot call a CheckingAccount method on a variable of
type BankAccount (can only use BankAccount
behavior)
BankAccount b = new CheckingAccount(0.10);
[Link]();
cannot use any account behavior on an Object variable
Object o = new CheckingAccount(0.06);
[Link]([Link]());
[Link]();
13