Title: CS12320 Lecture 16
1CS12320 - Lecture 16
- Reyer Zwiggelaar
- rrz_at_aber.ac.uk
2In This Lecture
3Terminology
- A subclass inherits from a superclass
- A subclass a derived class
- A subclass is a specialisation of the superclass
- A superclass a base class
- A superclass is a generalisation of a subclass
Stevens and Pooley, pp 22 - 24
4Definition
- A subclass is an extended, specialized version of
its superclass. It includes the operations and
attributes of the superclass, and possibly some
more.
Stevens and Pooley, pp 22 - 24
5Pure Versus Extension
- Pure only methods in the base class are to be
overridden in the derived class (see interfaces
and implements) - Subclass is a superclass
- Extension additional attributes and methods
allowed (see extends) - Subclass is like a superclass
Eckel, p 316
6extends
class SubClassName extends SuperClassName
//declaration of additional instance variables
//definitions of additional methods
Skansholm, p 223
7Generalisation
- .... relationship between a more general element
and a more specific element. The more specific
element is fully consistent with the more general
element (it has all of its properties, members
and relationships) and may contain additional
information.
Bennett et al., pp 68-73
8Generalisation - Example
Vehicle speed weight changeSpeed
Car enginePower changeGear
Train noCarriages coupleCar
Bicycle noGears pedal
Skansholm, p 102
9Composition
- Instead of inheritance it is possible to achieve
generalisation through using attributes of type
superclass - Does not use extends/implements
- This might be the first step to inheritance
- First composition then inheritance
- Typically indicated by a has a relationship
between classes
Eckel, pp 226/238/315
10Composition - Example
GearBox
Engine
Car gearbox engine changeSpeed()
Skansholm, p 102
11Coupling
- Criteria for Good Design
- Inheritance Coupling describes the degree to
which a subclass actually needs the features it
inherits from its superclass.
Bennett et al., p. 353
12Coupling - Poor Example
LandVehicle numberOfAxles registrationDate regis
ter()
Vehicle description serviceDate maximumAltitude t
akeOffSpeed checkAltitude() takeOff()
Bennett et al., p. 353
13Coupling - Improved Example
LandVehicle numberOfAxles registrationDate regis
ter()
Vehicle description serviceDate
Aircraft maximumAltitude takeOffSpeed checkAltit
ude() takeOff()
Bennett et al., p. 353
14Hierarchies
- Number of levels in class diagram
- Not too deep nor to shallow
- More than 4/5 levels is too much
- Others say that up to 9 levels might be
acceptable - Do not over-specialise
- Adds complexity
Bennett et al., p. 355/356
15Hidden Instance Variables
- An instance variable v in a subclass that has the
same name as an instance variable in the
superclass hides the instance variable in the
superclass - We can write super.v to access the instance
variable in the superclass
Skansholm, p 227
16Method Overriding
- Methods in the subclass with the same name and
parameters as in the superclass - A form of polymorphism
- The objects class determines which method is used
Skansholm, p 228, Eckel, p 231
17Method Overriding
class MySC String s From MySC void
get() println(s) class MyDC extends MySC
String s From MydC void get()
println(s) void getS() println(super.s)
Eckel, p 231
18Method Overloading
class MySC String s From MySC void
get() println(s) class MyDC extends MySC
String s From MydC void get()
println(s) void getS() println(super.s)
void get(String t) s t get()
Eckel, p 231
19Method Over
class Test static void main(String args)
MySC s new MySC() s.get() MyDC d
new MyDC() d.get() d.getS()
d.get("new text") d.get() d.getS()
C\gtjava Test From MySC From MydC From MySC new
text new text From MySC C\gt
Eckel, p 231
20Initialisation
- How Inheritance determines Attributes and Methods
- In which order are aspects executed
Eckel, p 246
21Subclass Constructor
- The instance variables for the superclass with
explicit initialization expressions are
initialized to these values - A constructor for the superclass is called
- The instance variables for the subclass with
explicit initialization expressions are
initialized to these values - The statements in the subclasss constructor are
executed
22Initialisation Example
public class Insect protected int i 9
protected int j Insect() prt(i
i , j j) j 39
protected int x1 prt(Insect.x1
initialized) public int prt(String s)
System.out.println(s) return 47
23Initialisation Example
public class Beetle extends Insect private
int k prt(Beetle.k initialized) Beetle()
prt(k k) prt(j j) private
int x2 prt(Beetle.x2 initialized) public
int prt(String s) System.out.println(s)
return 63 public static void
main(String args) System.out.println(Be
etle constructor) Beetle b new
Beetle()
24Initialisation Example
Beetle constructor Insect.x1 initialized i 9,
j 0 Beetle.k initialized Beetle.x2 initialized
k 63 j 39
25finalize()
- Method associated with the Object class and is
inherited by all classes - The method is called when an object is to be
garbaged - Can be redefined and used to clean up
- Needs to be called for the superclass
Skansholm, p 234
26clone
- To copy whole objects
- The method clone() needs to be publicly
implemented - Only available downwards in inheritance tree
Eckel, p 561
27Synchronized
- Just for information
- If a method is synchronized in the superclass it
is not necessarily synchronized in the subclass
overridden version
Eckel, p 785
28Books Used
- Skansholm Java From the Beginning
- Eckel Thinking in Java
- Stevens and Pooley Using UML
- Bennett, McRobb and Farmer Object-Oriented
Systems Analysis and Design
29In This Lecture
30In The Next Lecture