inheritance 1
Download
Skip this Video
Download Presentation
Inheritance #1

Loading in 2 Seconds...

play fullscreen
1 / 16

Inheritance #1 - PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Inheritance #1' - boaz


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
first questions
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
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

testing stepper
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
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
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

what is inherited
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
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
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

will this work
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!

must use a cast
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

will this work1
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?

incrementer in python
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()

testing incrementer
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

incrementer in java
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()

testincrementer in java
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

ad