ITI 1121. Introduction to Computing II – subtitle
ITI 1121. Introduction to Computing II
Inheritance: reusability
by
Marcel Turcotte
Version January 30, 2020
Preamble
Preamble
Overview
Overview
Inheritance: reusability
The concept of inheritance in Java promotes code reuse. Inheritance expresses a
parent-child relationship between two classes. The subclass has the attributes and
methods of the superclass. The subclass can also introduce new attributes and methods.
Finally, the subclass can redefine certain methods of the superclass.
General objective :
This week you will be able to structure a collection of classes hierarchically using
inheritance.
1 31
Preamble
Learning objectives
Learning objectives
Describe the functioning of a simple application using inheritance concepts.
Build a simple application from its specification and UML diagrams.
Criticize the use of the visibility modifier “protected”.
Lectures:
Pages 7–31, 39–45 of E. Koffman and P. Wolfgang.
2 31
Preamble
Plan
Plan
1 Preamble
2 Generalization/specialization
3 Example
4 Prologue
3 31
Generalization/specialization
Introduction
Object-oriented languages offer several mechanisms to structure programs.
Inheritance is one of these mechanisms and it favours the organization of classes in
a hierarchical way (in the form of a tree structure).
When we say that object-oriented programming favours code reuse we are referring
to the notion of inheritance.
4 31
Definitions: superclass and subclass
The class above in the inheritance tree is called the superclass (or parent), while the
class below is called subclass (also called derived class).
Bird
Pigeon
is a
In this example, Bird is the superclass of Pigeon, that is, Pigeon is a subclass of Bird.
5 31
Java: extends
In Java, the relationship “is a” is expressed using the reserved keyword extends.
pub l i c c l a s s Pigeon extends Bi rd {
. . .
}
Bird
Pigeon
is a
6 31
UML
In UML, the relationship “is a” is expressed using a full line connecting the child to the
parent and such that an open triangle points in the direction of the parent.
Bird
Pigeon
is a
7 31
Example: JComponent
A graphic element is called a graphic component. Consequently, there is a class
named JComponent, which defines the common characteristics of the components.
The subclasses of JComponent include: JLabel, JList, JMenuBar, JPanel,
JScrollBar, JTextComponent, etc.
JComponent
JLabel JList JPanel JTextComponent
JEditorPane JTextArea JTextField
8 31
javax.swing.JComponent
JLabel JList JPanel JTextComponent
JEditorPane JTextArea JTextField
java.awt.Container
java.awt.Component
Object
AWT and Swing make heavy use of inheritance. The class Component defines the
set of methods common to graphic objects, such as setBackground(Color c)and
getX().
The Container class defines the behavior of graphical objects that can contain other
graphical objects, the class defines the methods add(Component component) and
setLayout(LayoutManager mgr), among others.
What’s it for?
A class inherits the characteristics (variables and methods) of its superclass.
1. A subclass inherits the methods and variables of its superclass;
2. A subclass can introduce/add new methods and variables;
3. A subclass can redefine the methods of the superclass.
Since we can only add new elements, or redefine them, a superclass is more general than
its subclasses, and conversely, and conversely, a subclass is more specialized than its
superclass.
10 31
Example
Exemple: Shape
Problem: We need to create a set of classes to represent geometric shapes, such as
circles and rectangles.
All the objects must have two instance variables, x and y, which represent the
position of the object.
11 31
Example: Shape
Circle Rectangle
-x: double
-y: double
Shape
12 31
pub l i c c l a s s Shape {
p r i v a t e double x ;
p r i v a t e double y ;
pub l i c Shape ( ) {
x = 0 . 0 ;
y = 0 . 0 ;
}
}
13 31
pub l i c c l a s s Shape {
p r i v a t e double x ;
p r i v a t e double y ;
pub l i c Shape ( ) {
x = 0 . 0 ;
y = 0 . 0 ;
}
pub l i c Shape ( double x , double y ) {
t h i s . x = x ;
t h i s . y = y ;
}
}
Yes, yes, yes, yes, yes! Several methods (or constructors) may have the same name,
provided that the signatures of the methods (constructors) differ.
It’s called overloading.
Acces methods
pub l i c c l a s s Shape {
p r i v a t e double x ;
p r i v a t e double y ;
// . . .
pub l i c double getX ( ) {
re tu rn x ;
}
pub l i c double getY ( ) {
re tu rn y ;
}
}
15 31
Method toString
pub l i c c l a s s Shape {
p r i v a t e double x ;
p r i v a t e double y ;
// . . .
pub l i c double getX ( ) { re tu rn x ; }
pub l i c double getY ( ) { re tu rn y ; }
pub l i c S t r i n g t o S t r i n g ( ) {
re tu rn ” Located at : ( ” + x + ” , ” + y + ” ) ” ;
}
}
16 31
Example: Circle
pub l i c c l a s s C i r c l e extends Shape {
}
The above statement implies that the class Circle is a subclass of the class Shape.
All the objects of the class Circle will have two instance variables, x and y, as well as
the following methods, getX() and getY().
17 31
Example: Circle
pub l i c c l a s s C i r c l e extends Shape {
// I n s t a n c e v a r i a b l e
p r i v a t e double r a d i u s ;
}
18 31
Private versus protected
pub l i c c l a s s C i r c l e extends Shape {
p r i v a t e double r a d i u s ;
pub l i c C i r c l e ( double x , double y , double r a d i u s ) {
t h i s . x = x ;
t h i s . y = y ;
t h i s . r a d i u s = r a d i u s ;
}
}
Compiling the above will produce the following error “x has private access in Shape”
(similarly for y).
19 31
Protected
The compile time error message can be eliminated by declaring the variables protected in
the superclass Shape.
pub l i c c l a s s Shape {
protected double x ;
protected double y ;
// . . .
}
20 31
Private
I prefer to keep visibility of the variables private.
Forcing us to use the superclass’s access methods.
21 31
super
pub l i c c l a s s C i r c l e extends Shape {
p r i v a t e double r a d i u s ;
// C o n s t r u c t o r s
pub l i c C i r c l e ( ) {
super ( ) ;
r a d i u s = 0 ;
}
pub l i c C i r c l e ( double x , double y , double r a d i u s ) {
super ( x , y ) ;
t h i s . r a d i u s = r a d i u s ;
}
}
22 31
Super
The statement super(. . . ) is an explicit call to the constructor of the immediate
superclass.
This construct, super(. . . ), only appears in a constructor.
That call must be the first statement of the constructor.
A call of the form super() is automatically inserted unless you add a call super(. . . )
yourself?
23 31
pub l i c c l a s s C i r c l e extends Shape {
p r i v a t e double r a d i u s ;
pub l i c C i r c l e ( ) {
super ( ) ;
r a d i u s = 0 ;
}
pub l i c C i r c l e ( double x , double y , double r a d i u s ) {
super ( x , y ) ;
t h i s . r a d i u s = r a d i u s ;
}
pub l i c double ge tRad iu s ( ) {
re tu rn r a d i u s ;
}
}
Example: Rectangle
pub l i c c l a s s Rec tang l e extends Shape {
p r i v a t e double width ;
p r i v a t e double h e i g h t ;
pub l i c Rec tang l e ( ) {
super ( ) ;
w idth = 0 ;
h e i g h t = 0 ;
}
pub l i c Rec tang l e ( double x , double y , double width , double h e i g h t ) {
super ( x , y ) ;
t h i s . w idth = width ;
t h i s . h e i g h t = h e i g h t ;
}
}
25 31
Example: Rectangle
pub l i c c l a s s Rec tang l e extends Shape {
p r i v a t e double width ;
p r i v a t e double h e i g h t ;
// . . .
pub l i c double getWidth ( ) {
re tu rn width ;
}
pub l i c double ge tHe i gh t ( ) {
re tu rn h e i g h t ;
}
}
26 31
Usage
C i r c l e c , d ;
d = new C i r c l e ( 1 0 0 . 0 , 200 .0 , 1 0 . 0 ) ;
System . out . p r i n t l n ( d . ge tRad iu s ( ) ) ;
c = new C i r c l e ( ) ;
System . out . p r i n t l n ( c . getX ( ) ) ;
Rec tang l e r , s ;
r = new Rec tang l e ( ) ;
System . out . p r i n t l n ( r . getWidth ( ) ) ;
s = new Rec tang l e ( 5 0 . 0 , 50 . 0 , 10 . 0 , 1 5 . 0 ) ;
System . out . p r i n t l n ( s . getY ( ) ) ;
27 31
Prologue
Summary
Inheritance allows us to organize classes hierarchically.
The keyword extends in the signature of a class indicates its parent.
28 31
Next module
Polymorphism
29 31
References I
E. B. Koffman and Wolfgang P. A. T.
Data Structures: Abstraction and Design Using Java.
John Wiley & Sons, 3e edition, 2016.
30 31
Marcel Turcotte
Marcel.
School of Electrical Engineering and Computer Science (EECS)
University of Ottawa
31 / 31
Marcel.
Preamble
Overview
Learning objectives
Plan
Generalization/specialization
Example
Prologue