Title: Review
1Review
- View classes as modules
- Encapsulate operations
- View classes as struct types
- Encapsulate data
- View classes as abstract data types
- Encapsulate both data and operations
- Inheritance
- Defining classes in terms of other classes
2Inheritance
superclass
ParkingMeter
subclass or extended class
DigitalParkingMeter
AnalogParkingMeter
3Syntax
A
B
class B extends A ....
4An Example of Inheritance
- Variety of heating controllers.
- Basic (shared) functionality.
- On or off.
- More sophisticated functionality.
- Temperature setting.
- On/off time periods.
- Possibly others.
5HeaterController Super Class
class HeaterController public void
switchOn() setOn(true)
public void switchOff() setOn(false)
public boolean isOn() return
getOn() ...
6Extending HeaterController
class VariableController extends HeaterController
public static final int DefaultLevel 16,
... public int
getLevel() return getHeater().getTemperat
ure() public void setLevel(int level)
throws RuntimeException if((MinLevel lt
level) (level lt MaxLevel))
getHeater().setTemperature(level)
else throw new RuntimeException(
"Illegal level setting
"level)
7A VariableController Object
- Use like a VariableController or
HeaterController.
VariableController v new VariableController() .
.. // Warm up the room. v.switchOn() if(v.getLeve
l() v.MinLevel) v.setLevel(v.DefaultLevel)
... // The room is warm enough - switch the
heater off. v.switchOff()
8The Is-A Relationship
- Sub class/super class often characterized in this
way. - VariableController is a HeaterController.
- Circle is a Shape, Rectangle is a Shape, Square
is a Rectangle. - The relationship is one way.
- Rectangle is not necessarily a Square.
9Is-A versus Has-A
- A common linguistic confusion.
- Often responsible for inappropriate class
structures. - VariableController has a level attribute.
- VariableController is a HeaterController.
10Issues in Inheritance
- Inappropriate Inheritance.
- Multiple Inheritance.
- Downcasts and Upcasts.
- Inheritance for Specialization.
- Structural Similarity of Classes.
- Order of Initialization and Super Class
Constructors.
11Inappropriate Inheritance
import java.util.Random // Inappropriate
inheritance class Die extends Random //
Return an integer in the range 1..6 public
int roll() final int range 6
// Use the inherited nextInt method.
return 1nextInt(range)
- java.util.Stack extends java.util.Vector
12Multiple Inheritance?
- A class may extend only a single class.
- A class may implement multiple interfaces.
- In addition to extending a single class.
- An interface may extend multiple interfaces.
- interface ParentTeacher extends Parent, Teacher
13Downcasts and Upcasts
- The terminology relates to the inheritance
hierarchy up or down.
// Explicit downcasting. // Downcast Object -gt
String. String s (String)h.get(key) //
Implicit upcasting VariableController v new
VariableController() // Upcast HC lt-
VC. HeaterController h v
14Inheritance for Specialization
- Sub classes are often more specialized versions
of their super classes. - President extends Citizen
- A supermarket's SellableItem class extended to
add a sell-by date. - class PerishableItem extends SellableItem
- Sub class has everything (and more).
15Sub Class Initialization
- The super class elements of a sub class object
must be properly initialized. - VariableController on/off state.
- Super class elements initialized first.
- Super class constructor selection is necessary.
- Argument passing must be arranged.
16Super Class Construction (cont.)
- super must be called as first statement.
- No-arg super class constructor called by default.
- Sub class error if the super class does not have
one. - All classes without any constructor have a
default no-arg constructor.
17Default No-Arg Constructor
class VariableController extends HeaterController
public VariableController(int initialLevel)
throws ... setLevel(initialLevel)
... // Implicit extension of Object super
class. class HeaterController extends Object
// Default (implicit) no-arg constructor.
public HeaterController() // (Implicit)
super() ...
18Access Control Issues
- Inheritance means we need to revisit issues of
access control. - What rights does a sub class have over its super
class members? - Classes in one package might extend classes
defined elsewhere.
19Access Control Rules
- public access is global.
- private access is class only.
- Sub classes have no rights.
- package access is whole package.
- protected access is package plus sub classes.
20Overriding Methods
- Sub class specializations often need to modify
super class behavior. - The default behavior of equals only mimics
reference equality (). - clone only creates a shallow copy, not a deep
copy.
21class Point ... public String
toString() return "("getX()","getY()")"
public boolean equals(Object o) if(o
this) return true else
if(o null) return false
else if(o instanceof Point) Point
p (Point) o return getX()
p.getX() getY() p.getY() else
return false ...
22Rules on Overriding
- Return type and arguments must match.
- Sub class version must not be less visible.
- final disregarded in matching arguments.
- Checked exceptions in sub class must be
compatible with those in super class. - Sub class version may throw none.
23Method Selection
- Messages always arrive at the outermost layer for
non-private methods. - The outermost matching method is selected.
- Compilers generate efficient code to handle
selection. - An inner version may be selected.
24Selecting the Super Class Version of a Method
public void method(int arg) ... //
Invoke the closest super class version.
super.method(arg) ...
25Usage of super
- Distinct from usage of super in constructors.
- May be used from anywhere within a method.
- Only possible to select the nearest version from
an inner layer.
26Restrictions on Overriding
- Overridden method may not be private.
- Method signatures must have identical name,
return type, argument types. - Hence, Object argument of equals.
- Sub class version may be more visible.
- Sub class version's exceptions must be a subset
of those thrown by super class version.
27Super Class Behavior and Overriding
- When you invoke a method on an object, the actual
class of the object governs which implementation
is used - When you access a field, the declared type of the
reference is used
28Example
class CC2 extends C int getX() return
x2 int x2 2 class TestExtend
public static void main(String s) C o1
new CC1() C o2 new CC2()
System.out.println(o1.toString())
System.out.println(o2.toString())
class C public String toString()
return "x"getX() int getX() return
defaultX int defaultX 0 class CC1
extends C int getX() return x1 int
x1 1
29Example
class CC2 extends C int getX() return
x int x 2 class TestExtend
public static void main(String s) C o1
new CC1() C o2 new CC2()
System.out.println(o1.toString())
System.out.println(o2.toString())
class C public String toString()
return "x"x int getX() return x
int x 0 class CC1 extends C int
getX() return x int x 1
30Example
class CC2 extends C int getX() return
x int x 2 class TestExtend
public static void main(String s) C o1
new CC1() C o2 new CC2()
System.out.println(o1.x((CC1)o1).x)
System.out.println(o2.x((CC2)o2).x)
class C public String toString()
return "x"getX() int getX() return
x int x 0 class CC1 extends C
int getX() return x int x 1
31Final Classes and Methods
- A class defined as final cannot be sub classed.
- public final class String ...
- Often done to protect complex internal state
representations. - A method defined as final may not be overridden.
32Abstract Classes and Methods
abstract class Name ...
- Abstract classes can be extended but not
instantiated - A class that contains an abstract method must be
also abstract