Title: CS10092 : Inheritance in Java
1CS10092 Inheritance in Java(2 of 4)
Gavin Brown gavin.brown_at_manchester.ac.uk www.cs.
man.ac.uk/gbrown/teaching/java/
2REMINDER OF LAST TIME
The class we have written has the header public
class PorscheVersionTwo extends Porsche This is
now called a subclass of the Porsche class. The
extends Porsche part makes sure that Java
recognises it as so. The Porsche class is said
to be the superclass of the PorscheVersionTwo
class.
Super-class
Porsche
PorscheVersionTwo
Sub-class
3REMINDER OF LAST TIME
public class PorscheVersionTwo extends Porsche
private double percentBoost public void
accelerate(int mph) speedspeedmph(mph
percentBoost) public void
setTurboBoost( double perc )
percentBoost perc
- - The important bit is extends Porsche that
makes inheritance happen. - - This class INHERITS all the methods/variables
of the superclass. - as if they were copied down into the
subclass, automatically. - The accelerate method here OVERRIDES the one
from the superclass. - The setTurboBoost method ADDS EXTRA
FUNCTIONALITY.
4REMINDER OF LAST TIME
public class Bicycle private int
numberOfGears private int numberOfWheels publ
ic void turn( double degrees ) //code for
turning corners // more methods
public class MountainBike extends
Bicycle private double suspensionRatio //
more methods
5REMINDER OF LAST TIME
The subclasses inherit the type of their
superclass. Q. What type is
MountainBike? This is called polymorphism.
6Today
- Design variables in the hierarchy?
- More polymorphism making use of it
- Casting to subclasses
- Checking the type of an object at runtime
- Superclass superconstructor
- abstract classes
7Design Where do variables go?
Vehicle
class Vehicle int numberOfWheels class
Porsche extends Vehicle String numberPlate
double currSpeed double nationalSpeedLimit cla
ss Bicycle extends Vehicle int
numberOfGears class RacingBike extends Bicycle
String sponsorName
Bicycle
Porsche
RacingBike
Variables in COMMON go at the top of the
hierarchy. Variables specific to a class go in
that class. The same applies for methods.
8Making use of polymorphism
Polymorphism (inheriting the type) makes this
code possible
Vehicle v1 new Porsche() Vehicle v2 new
Bicycle() Vehicle v3 new Plane() System.out.p
rintln(v1.getNumberOfWheels()) System.out.println(
v2.getNumberOfWheels()) System.out.println(v3.getN
umberOfWheels())
Lets run that code
java test 4 2 6
9Making use of polymorphism
Or even more useful, an array of Vehicle objects
Vehicle vehicleList new Vehicle4 vehicleL
ist0 new Porsche() vehicleList1 new
Bicycle() vehicleList2 new
Plane() vehicleList3 new Porsche() for
(int i0 iListi.getNumberOfWheels())
Lets run that code
java test 4 2 6 4
10Be careful with types
vehicleList3 new Porsche() Vehicle v
vehicleList3 System.out.println(
v.getNumberOfWheels() ) //ok System.out.println(
v.getNumberPlate() ) //ERROR
(because getNumberPlate() is part of the Porsche
class, not the Vehicle class)
javac test.java test.java12 cannot resolve
symbol symbol method getNumberPlate()
location class Vehicle
System.out.println( v.getNumberPlate() )
1 error
11Be careful with types casting to the subclass
The object has to be cast to the subclass first
Porsche p (Porsche)vehicleList3 System.out.pr
intln( p.getNumberPlate() ) //ok!
The word Porsche in brackets tells Java which
type we want to cast to.
javac test.java java test STERED
12Checking the type of an object
Vehicle vehicleList new Vehicle4 vehicleL
ist0 new Porsche() vehicleList1 new
Bicycle() vehicleList2 new
Bicycle() vehicleList3 new Porsche() for
(int i0 iyour array position i is) if
(vehicleListi instanceof Porsche)
System.out.println( a fast car!) if
(vehicleListi instanceof Bicycle)
System.out.println( my bike.)
13Checking the type of an object
javac test.java java test In your array
position 0 is a fast car! In your array position
1 is my bike. In your array position 2 is my
bike. In your array position 3 is a fast car!
14Superclasssuperconstructor
java test
public class Porsche private String
numberPlate public Porsche( String plate )
numberPlate plate
public class PorscheVersionTwo extends Porsche
public PorscheVersionTwo( String plate )
super(plate) setNumberPlate(
numberPlate )
15Superclasssuperconstructor
PorscheVersionTwo p2 new PorscheVersionTwo(cdc
9922) System.out.println(p2.getNumberPlate())
java test -cdc-9922-
Call to superconstructor MUST be first!
public class PorscheVersionTwo extends Porsche
public PorscheVersionTwo( String plate )
super(plate) setNumberPlate(
numberPlate )
163 minutes. How many wheels on a bike?
Q. It makes sense to have a Porsche object,
and also a MountainBike object. Does it make
sense to have a Vehicle object?
17Porsche p new Porsche() RacingBike rb new
RacingBike() Vehicle v new Vehicle() //
????
Technically correct, but what does it mean?
18Abstract class Vehicle
Provides data and methods common to all Vehicles
public abstract class Vehicle private int
numberOfWheels public int getNumWheels()
return numberOfWheels public
abstract void turn()
And forces ALL subclasses to have a turn()
method
Abstract classes Useful software engineering
tool when working in a team, write an abstract
class and give it to a colleague to work from.
You can provides some functionality, and impose
some rules, like the above class making the rule
that subclasses should have a turn() method.
19Extending from the abstract Vehicle class
public class Plane extends Vehicle private
double wingspan public void takeOff()
// more code
javac Plane.java Plane.java1 Plane should be
declared abstract it does not define turn() in
Vehicle public class Plane extends Vehicle
1 error
Here, the compiler is telling us that the Plane
should be made abstract, because we do not define
turn(). In fact, we should just define the
turn() method and all will be fine. The compiler
gave the best advice it could, assuming that we
wanted Plane to be abstract, and forgot to do so.
20Today
- Design variables in the hierarchy
- More polymorphism making use of it
- Casting to subclasses
- Checking object type instanceof
- Superclass superconstructor
- Abstract classes
- Next time
- How to dynamically choose between methods at
runtime - How inheritance can be used to hack a system and
how to stop it - How Accenture built inheritance into the
Sainsburys IT system