CS12320 Lecture 16 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS12320 Lecture 16

Description:

A subclass inherits from a superclass. A subclass = a derived class. A subclass is a specialisation of the superclass. A superclass = a ... pedal. Composition ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 31
Provided by: author4
Category:
Tags: cs12320 | lecture | pedal

less

Transcript and Presenter's Notes

Title: CS12320 Lecture 16


1
CS12320 - Lecture 16
  • Reyer Zwiggelaar
  • rrz_at_aber.ac.uk

2
In This Lecture
  • Inheritance

3
Terminology
  • 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
4
Definition
  • 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
5
Pure 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
6
extends
class SubClassName extends SuperClassName
//declaration of additional instance variables
//definitions of additional methods
Skansholm, p 223
7
Generalisation
  • .... 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
8
Generalisation - Example
Vehicle speed weight changeSpeed
Car enginePower changeGear
Train noCarriages coupleCar
Bicycle noGears pedal
Skansholm, p 102
9
Composition
  • 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
10
Composition - Example
GearBox
Engine
Car gearbox engine changeSpeed()
Skansholm, p 102
11
Coupling
  • 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
12
Coupling - Poor Example
LandVehicle numberOfAxles registrationDate regis
ter()
Vehicle description serviceDate maximumAltitude t
akeOffSpeed checkAltitude() takeOff()
Bennett et al., p. 353
13
Coupling - Improved Example
LandVehicle numberOfAxles registrationDate regis
ter()
Vehicle description serviceDate
Aircraft maximumAltitude takeOffSpeed checkAltit
ude() takeOff()
Bennett et al., p. 353
14
Hierarchies
  • 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
15
Hidden 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
16
Method 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
17
Method 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
18
Method 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
19
Method 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
20
Initialisation
  • How Inheritance determines Attributes and Methods
  • In which order are aspects executed

Eckel, p 246
21
Subclass 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

22
Initialisation 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
23
Initialisation 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()
24
Initialisation Example
Beetle constructor Insect.x1 initialized i 9,
j 0 Beetle.k initialized Beetle.x2 initialized
k 63 j 39
25
finalize()
  • 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
26
clone
  • To copy whole objects
  • The method clone() needs to be publicly
    implemented
  • Only available downwards in inheritance tree

Eckel, p 561
27
Synchronized
  • Just for information
  • If a method is synchronized in the superclass it
    is not necessarily synchronized in the subclass
    overridden version

Eckel, p 785
28
Books 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

29
In This Lecture
  • Inheritance

30
In The Next Lecture
  • Input/Output
Write a Comment
User Comments (0)
About PowerShow.com