Lecture3
Lecture3
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:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is Inheritance?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
1
What is Class Inheritance?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Terminology
• Parent • Child
• Base • Derived
• Generalised • Specialised
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Multiple Inheritance
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
implements)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
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
Methods:
– area() = l*w
– perimeter() = 2l+ 2w
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
4
Example: Square
Attributes:
– Length
Methods:
– area() = l*l
– perimeter() = 4l
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is different?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
Implementation Inheritance
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
Super
The keyword super is used to call the parent class.
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:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
For Example
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
Overriding
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?
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
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
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);
this.width = width;
this.height = 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
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
10
Example: Shape
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
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Quick Quiz
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