Inher
Inher
1 2
• Suppose you are the client. After receiving puzzle code, • Three approaches:
you decide you want the code to keep track of the number
of moves made since the last scramble operation. Call supplier and send them a new specification.
They implement it and charge you an extra $5K.
• Implementation is simple:
keep a counter numMoves, initialized to 0 Rewrite the supplier’s code yourself.
method move should increment the counter
method scramble should reset the counter to 0
new method printNumMoves for printing value of counter Use inheritance to define a new class that extends
the behavior of the supplier’s class.
5 6
Goal Picture
7 8
11 12
scramble and move
Writing the EPuzzle Class
How should we write scramble and move?
One option: write them from scratch.
class EPuzzle extends Puzzle {
Class EPuzzle extends Puzzle {
private int numMoves = 0;
...
public void printNumMoves() {
public void scramble() {
System.out.println("Number of moves = "
+ numMoves); state = "978654321";
numMoves = 0;
}
}
public void scramble() {...}
public boolean move(char d) {...} public boolean move(char d) {...}
}
}
Protected Access
Proper Code for Puzzle Class
• Access specifier: protected
• A protected instance variable in class S can be says state is
accessed by instance methods defined in S or in any accessible from
subclass of S subclasses
class Puzzle {
• A protected method in class S can be invoked from an
protected int state;
instance method defined in S or any subclass of S
public void scramble(){...}
• Access checks are done by compiler at compile time: ...
For an invocation r.m(): }
• Determine type of reference r
• Does the corresponding class/interface have a method named
m with appropriate arguments?
• Are the access specifiers of that method appropriate?
17 18
Code for EPuzzle Protected Access
class EPuzzle extends Puzzle { • When should variables and methods be declared
...
protected instead of private?
public void scramble() { • Think about extensibility: if subclasses will want
state = "978654321"; //OK since state inherited access to a member, it should be declared
numMoves = 0; protected
}
• Analogy:
//similar code for move Which components of a car might a user want to upgrade?
} What wires/subsystems need to be exposed to make the
upgrade easy?
19 20
21 22
class B extends A {
• Examples: private int m() {...} //illegal!
Puzzle p = new EPuzzle(); //up-casting }
EPuzzle e = (EPuzzle)p; //down-casting
A supR = new B(); //upcasting
supR.m(); //would invoke private method in
class B at runtime!
23 24
Shadowing Variables Constructors
• Like overriding, but for fields instead of methods
Superclass: variable v of some type
• Each class has its own constructor
Subclass: variable v perhaps of some other type
Method in subclass can access shadowed variable using super.v
• No overriding of constructors
• Superclass constructor can be invoked explicitly
• Variable references are resolved using static binding (i.e., at
within subclass constructor using super() with
compile-time), not dynamic binding (i.e., not at runtime) parameters as needed
Variable reference r.v uses the static type (declared type) of the • Can invoke other constructors of the same class
variable r, not the runtime type of the object referred to by r using this()
• Call to super() or this() must occur first in the
• Shadowing variables is bad medicine and should be avoided constructor
25 26
29 30