Information Technology
Applications Programming
LECTURE 3
Inheritance
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Inheritance
Learning Objectives
At the end of the lecture, you should be able to:
Describe inheritance, giving examples of both types
Define Overriding
Differentiate between interfaces and abstract classes
Describe ways a child class can access parent methods
WU Chapter 13.1-13.2 & 13.4-13.7
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is Inheritance?
Inheritance is the passing on of characteristics or attributes
from one entity to another
A child inherits certain characteristics e.g. eye colour, from a
parent
Now lets look at Class Inheritance….
Remember: A class has a set of characteristics
• methods
• attributes
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
1
What is Class Inheritance?
Class Inheritance allows a new (child) class to be created based
on an existing (parent) class.
- The child (or derived) class inherits all characteristics of the
parent (or base) class.
The derived (or specialised) class inherits methods and
attributes from the base (or generalised) class
A base class never does more than a derived class
That is, a parent can not do more than a child
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Terminology
Super Class Sub Class
• Parent • Child
• Base • Derived
• Generalised • Specialised
Inheritance creates an “is a” relationship between 2
classes
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Multiple Inheritance
Multiple Inheritance – where a child class inherits
from more than one parent class.
Java handles this in a unique way
Implementation
Inheritance
• Parent class has coded
methods, child class extends
Interface Inheritance
the code • Child class implements methods
defined by the Interface
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
2
Types Of Inheritance
• Inherits all methods & attributes from the
Implementation parent class
Inheritance • class A extends B
• There can only be one parent class
(keyword
extends)
• Inherits from an interface (skeleton – empty
Interface methods & constant attributes only)
Inheritance • class A implements InterfaceB
• A class can inherit from many interfaces
(keyword • Example: Class A extends B implements C, D
implements)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
The “Is A” Relationship
A Ballerina “is a” Dancer
A ballerina wears a tutu and dances on
her toes. To call a ballerina a dancer,
would be a generalisation. A ballerina is a
special type of dancer.
A Diamond “is a” Gemstone
A diamond is a specific type of precious
gemstone. It has all the properties of a
gemstone, plus some additional
characteristics. So, a diamond is a
specialised type of gemstone.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Implementation Inheritance Example
A square “is a” rectangle
A square is a specialised type of rectangle
– A rectangle is a shape with 4 sides and 4 right angles
A square has all the properties of a rectangle plus some
unique properties
– A square has 4 equal sides
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
3
Class Inheritance Diagram
Rectangle
SIDES = 4
ANGLE = 90
length
width
area()
perimeter()
Square
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Example: Rectangle
A Rectangle “is a” shape, with 4 sides and 4 right angles.
Attributes: (SIDES and ANGLE are not used, so don’t write
them).
– width:
– length:
Methods:
– area() = l*w
– perimeter() = 2l+ 2w
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
The Rectangle Class
public class Rectangle
{
private double length;
private double width;
public Rectangle(double length, double width)
{
[Link] = length;
[Link] = width;
}
public double area()
{
return length*width;
}
public double perimeter()
{
return 2*(length + width);
}
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
4
Example: Square
A square “is a” rectangle with 4 equal sides
Attributes:
– Length
Methods:
– area() = l*l
– perimeter() = 4l
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
The Square Class
public class Square
{
private double length;
public Square(double length)
{
[Link] = length;
}
public double area()
{
return length*length;
}
public double perimeter()
{
return 4*length;
}
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Rectangle & Square
Now look at the 2 classes
Notice the similarities:
– Attributes
– Methods
What is different?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
Implementation Inheritance
Implementation Inheritance: keyword extends
Inherits all methods & attributes from the parent class
class A extends B
There can only be one parent class
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Rewrite Square to Inherit
Use keyword extends
public class Square extends Rectangle
Square no longer needs attributes
– length is defined in Rectangle, so is no longer needed in
Square because Square already has this attribute through
inheritance
Private attributes in Rectangle need to be protected
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Accessing Parent Methods
• Call a parent method – this
is called Vanilla Inheritance
A child • Effect (provide
implementation of) a parent
may method – if it is abstract
• Redefine an effective parent
method (override)
A child method cannot have less visibility than its parent
– If the parent is public than the child must be public.
– If the parent is protected the child must be public or protected.
A child method should do more than its parent
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
Super
The keyword super is used to call the parent class.
• [Link]() calls the
parent method show()
• super(a, b) is used to
call a parent
Super constructor with 2
arguments
• super() calls the parent
default constructor
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Quick Quiz
What is the keyword used to call the parent class?
Consider the following parent constructor:
public Parent(int id, Customer customer)
{
[Link] = id;
[Link] = customer;
}
And the following child class constructor
public Child(int id, Customer customer)
{…}
How do I call the parent constructor ?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Derived Class Constructors
Usually, the derived (or child) class constructor(s) have the
same parameter list as the parent, or contain additional
parameters.
For Example
Parent(int id, Customer customer){..}
Child(int id, Customer customer, String name) {..}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
Overriding
A child may override a parent method
– The child class can provide an alternate implementation
for a parent’s method
– Method signature must be the same
– A child must override an abstract method in the parent
class
– A parent method cannot be overridden if it is declared
final
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Overriding Vs Overloading
Overloading Overriding
• Method • Method
signature cannot signature must
be the same be the same
• Multiple • 1 method,
methods with multiple
same name implementations
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Quick Quiz
What is the term for having multiple methods with the same
name in a class?
When a subclass provides a different implementation for a
parent method, what is this called?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
8
Concrete Class
ConcreteA concrete
Class class is a standard class:
• Can be instantiated (create objects by calling the
constructor)
• Can be sub classed
• Contains only implemented methods
• Can contain changeable attributes
So far, we have only looked at concrete classes.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Abstract Class
Abstract Class
• May contain abstract methods (method
declarations without implementation).
• Cannot be instantiated but it can be sub-classed.
• May contain some implemented methods.
• May contain changeable attributes
If a class contains an abstract method the class must be declared
abstract
If an abstract class only contains abstract methods it should be
declared as an interface
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
More on Abstract Classes
An Abstract class usually has one or more Abstract Methods.
Classes which inherit from it need to provide some effective
methods but some methods will already be implemented.
Therefore, all classes that inherit from a particular abstract
class have some common code
Classes which inherit from an abstract class use
Implementation Inheritance
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
9
public abstract class Door //notice the keyword abstract in the class declaration
{
private double height;
private double width;
private boolean locked = false;
public Door(double height, double width)
{ //you cannot create an object from this class directly ie new Door(10.0, 5.0);
[Link] = width;
[Link] = height;
}
public void lock()
{
locked = true;
}
public void unlock()
{
locked = false;
}
public double area()
{
return height*width;
}
public abstract void open(); //notice the missing implementation
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Interface
Interface
• is a special type of class-like structure
• only contains constant declarations and method signatures.
• Cannot be instantiated (ie no objects)
• Can only be implemented by another class or extended by another
interface
• An interface contains only abstract methods.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
More on Interface
• An Interface has all abstract methods
• Classes that inherit from an interface must provide all the
effective methods
Therefore, all classes that inherit from an interface effect
the same methods (have the same method signatures) but
have no common code.
Classes which inherit from an interface use Interface
Inheritance
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
10
Example: Shape
A shape has an area and a perimeter.
Methods:
• area()
• perimeter()
There is no generic formula for the area or perimeter of a
shape, so the methods should be abstract.
Since there are no other methods or attributes – shape should
be an interface.
A circle, rectangle and triangle are all shapes
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Interface Vs Abstract
Interface Abstract
• Cannot be instantiated • Cannot be instantiated
• Only contains abstract • Can contain abstract
methods methods
• Cannot contain • Can contain
changeable variables changeable variables
• Uses Interface • Uses Implementation
Inheritance Inheritance
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Quick Quiz
With a partner, answer these questions:
– What is one difference between an interface and an
abstract class ?
– What do abstract classes and interfaces have in common ?
– Why can’t the Door class be an Interface ?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
11
Design Steps
1. Identify
3. Create
objects
group classes
• create classes
• implement list
lookup pattern • add attributes
and methods
2. Find
common
behaviour
• refactor for
parent class
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Key Concepts
Summary
A child object can always be
used in place of a parent
object, because the child has a
parent object (remember - it
calls the parent constructor).
This is the basic behaviour of
inheritance: the child can do
everything the parent can plus
more.
A child can never do less than
its parent.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
12