Inheritance #1
First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members of its parent? Is the inheritance hierarchy reflected in type declaration? How does method overriding work?
Stepper in Python from Counter import Counter class Stepper (Counter): # from Python Review #1 def __init__(self): Counter.__init__(self) def reset(self): self.value = 0 # parent’s constructor called explicitly # where is my parent? # child method; others inherited
Testing Stepper # Testing the child class: s = Stepper() print "initial value", s.current() s.step() print "after one step", s.current() s.reset() print "after reset", s.current() # inherited # new method
Stepper in Java public class Stepper extends Counter { public Stepper () { super(); } public void reset() { this.value = 0; } // parent's constructor called implicitly // child method; others inherited
Recall Java’s Counter public class Counter { private int value; public Counter () { this.value = 0; } public void step() { this.value += 1; } public int current() { return this.value; } // encapsulation enforced // static typing
What is inherited? Methods are public – they are inherited Instance variable “value” is private – it is not inherited So how do we access “value” in Stepper?
Use protected visibility public class Counter { protected int value; public Counter () { this.value = 0; } public void step() { this.value += 1; } public int current() { return this.value; } // only “package” encapsulation enforced // child class has access
TestStepper in Java public class TestStepper { public static void main (String[] args) { // Testing the child class: Stepper s = new Stepper(); System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); s.reset(); System.out.println("after reset " + s.current()); } // inherited // new method
NO! Will this work? public class TestStepper { public static void main (String[] args) { // Testing the child class: Counter s = new Stepper(); System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); s.reset(); System.out.println("after reset " + s.current()); } // Stepper is a “kind of” Counter // no method reset() in class Counter!
Must use a cast public class TestStepper { public static void main (String[] args) { // Testing the child class: Counter s = new Stepper(); // Stepper is a “kind of” Counter System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); ((Stepper)s).reset(); System.out.println("after reset " + s.current()); } // tell javac “s” is actually a Stepper object
public class TestStepper { public static void main (String[] args) { // Testing the child class: Stepper s = new Counter(); System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); s.reset(); System.out.println("after reset " + s.current()); } Will this work? // Counter a “kind of” Stepper? NO!
Incrementer in Python # parent’s constructor called explicitly # child instance variable # overrides parent step() Recall this method overriding example: from Counter import Counter class Incrementer (Counter): # from Python Review #1 def __init__(self, increment=1): Counter.__init__(self) self.increment = increment def step(self): self.value += self.increment
Incrementer (child) 0 : 3 Incrementer (child) 1 : 6 Counter (parent) 0 : 1 Counter (parent) 1 : 2 Testing Incrementer # Testing the parent class: obj = Counter() for i in range(2): obj.step() print "Counter (parent)", i, ":", obj.current() # Testing the child class: obj = Incrementer(3) for i in range(2): obj.step() print "Incrementer (child)", i, ":", obj.current()
Incrementer in Java public class Incrementer extends Counter { private int increment; public Incrementer (int increment) { super(); this.increment = increment; } public Incrementer () { super(); this.increment = 1; } public void step() { this.value += this.increment; } // constructor for default increment // overrides parent step() // child instance variable // constructor for increment Overloaded!
public class TestIncrementer { public static void main (String[] args) { Counter obj; // Testing the parent class: obj = new Counter(); for (int i = 0; i < 2; i++) { obj.step(); System.out.println("Counter (parent) " + i + ":“ + obj.current()); } // Testing the child class: obj = new Incrementer(3); for (int i = 0; i < 2; i++) { obj.step(); System.out.println("Incrementer (child) " + i + ":“ + obj.current()); } Counter (parent) 0 : 1 Counter (parent) 1 : 2 TestIncrementer in Java P o l y m o r p h i s m ! Incrementer (child) 0 : 3 Incrementer (child) 1 : 6