Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Supplementary reading:
Java text (e.g. Budd Ch. 8, 11)
Java tutorial
tutor al (http //www.cs.auckland.ac.nz/JavaCache/tutor al)
http://www.cs.auckland.ac.nz/JavaCache/tutorial
Trail: “Learning the Java Language”
Lessons: “Class and inheritance”,
“Interfaces and packages”
Forms of inheritance
More on Inheritance
Subclassing for specialisation
withdraw( ) only decrements the
balance if it stays in credit
The subclass alwaysy BankAccount
satisfies the balance : int
specification of its deposit( amount : int ) : void
superclass; i.e. the withdraw( amount : int ) : boolean
subclass is a subtype
In many cases properties
GoldAccount
are only added by the
subclass; inherited f
overdraftLimit : int
Implements paint( )
Implements paint( )
More on Inheritance COMPSCI 230 Software Design & Construction and move( ) 4
More on Inheritance
Subclassing for construction
Class java.util.Vector Vector
implements the List
add( index : int, obj : Object ) : void
interface
This is code reuse, …
Subclassing for
construction class Vector {
private:
// Some sort of linear data structure.
…
In Java, a subclass can’t public:
void add( int index
index, Object*
Object obj ) {
drop methods inherited // Add obj at specified index.
}
from its superclass }
p
superclass for }
implementation
purposes only
Vector* v = new Stack( );
8
Stack* s = new Stack( ); 9
s->add( … ); 8
s->push( … ); 9
More on Inheritance COMPSCI 230 Software Design & Construction 6
More on Inheritance
Subclassing for limitation
This is similar to subclassing class Stack extends Vector {
f construction – typically
for ll public
bl void d push(
h( Object
Ob obj
b ){
// Always add to the start
where a library class is // of the vector.
reused as a convenience add( 0, obj );
}
means of implementing a new …
class public void add( int index, Object obj ) {
// Eliminate this method by throwing
With subclassing for // an unchecked exception.
limitation, the subclass throw new
pp p p
UnsupportedOperationException( ( );
)
overrides inherited }
behaviour that is }
inappropriate to eliminate it
able
bl to define
d f a new class
l by
b enrol( ) teach( )
sonny : Tutor
enrol( )
teach( )
More on Inheritance COMPSCI 230 Software Design & Construction 8
More on Inheritance
Inheritance & instantiation
We know that a constructor 1: execute code
j
in Object’s
is always
l s executed
x cut d when
h n constructor
instantiating a class Object
Where inheritance is
involved, a constructor
method for each class in the
inheritance chain – between
the class being instantiated PlayingCard
More on Inheritance
Inheritance & destruction /
finalization public class Logger {
classes
interface that defines methods (7) WindowListener
concerning the lifecycle of a window
windowClosing( )
…
More on Inheritance
Static typing in OOPLs << abstract >>
Shape
move( ) : void
paint( ) : void
toString( )
More on Inheritance
Run-time type determination
Since an object reference can, in
general, point to many classes of Object
object, OOPLs offer a means to
determine what class of object a
reference
f n currentlyntl p
points
ints tto
Java provides the instanceof
operator << abstract >>
Shape
instanceof evaluates to true if the
left operand refers to an instance
of the class (or any of its
subclasses) identified by the right
hand operand, false otherwise
RectangleShape
Object
Casting
Casting is often Movie
object’s class
Movie instance
obj.getTitle( )
8 Movie m = ( Movie )obj;
compile-time error
m.getTitle( );
9
Casting does not change the object that is pointed to in any way.
More on Inheritance
Casting
Casting is potentially To check that a
dangerous potential cast is safe,
Movie instance the instanceof operator
obj : Object
should be used
Movie m = ( Movie )obj; 9 Compile time
Object-ref instanceof Class
8 Run-time
obj : Object
A ClassCastException is thrown if the
type cast is incompatible with the type of
object to be pointed to. ActionMovie instance
Object Movie
m : Movie ActionMovie instance
instance
m3 : Movie
m2 : Movie str : String String
DramaMovie instance
Movie String
instance
title : String
Compile-time Run-time
getTitle( ) : String behaviour behaviour
(ActionMovie)m; 9 9
(ActionMovie)str; 8
ActionMovie DramaMovie
(ActionMovie)m2; 9 8
Ref. var (ActionMovie)m3; 9 8
Cast-type
(Movie) m4; 9 9
At compile-time, checking is carried out to determine whether the cast-type and the
type
More of the existing reference
on Inheritance COMPSCIvariable are Design
230 Software related through
and inheritance.
Construction 18
More on Inheritance
Casting: run-time checking m4 : ActionMovie
Object Movie
m : Movie ActionMovie instance
instance
m3 : Movie
m2 : Movie str : String String
DramaMovie instance
Movie String
instance
title : String
Compile-time Run-time
getTitle( ) : String behaviour behaviour
(ActionMovie)m; 9 9
(ActionMovie)str; 8
ActionMovie DramaMovie
(ActionMovie)m2; 9 8
Ref. var (ActionMovie)m3; 9 8
Cast-type
(Movie) m4; 9 9
At run time, a check is made to determine whether the object actually pointed to is
compatible with the cast-type.
More on Inheritance COMPSCI 230 Software Design and Construction 19
More on Inheritance