1 / 16

Inheritance #1

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.

boaz
Download Presentation

Inheritance #1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance #1

  2. 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?

  3. 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 # where is my parent? # parent’s constructor called explicitly # child method; others inherited

  4. 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

  5. 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

  6. 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; } } // static typing // encapsulation enforced

  7. 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?

  8. 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

  9. 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

  10. 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! // no method reset() in class Counter!

  11. 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

  12. Will this work? 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()); } } NO! // Counter a “kind of” Stepper?

  13. Incrementer in Python 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 # parent’s constructor called explicitly # child instance variable # overrides parent step()

  14. 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() Counter (parent) 0 : 1 Counter (parent) 1 : 2 Incrementer (child) 0 : 3 Incrementer (child) 1 : 6

  15. 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; } } // child instance variable // constructor for increment // constructor for default increment Overloaded! // overrides parent step()

  16. TestIncrementer in Java 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 Polymorphism! Incrementer (child) 0 : 3 Incrementer (child) 1 : 6

More Related