25
COMP 401 INHERITANCE – INHERITED VARIABLES AND CONSTRUCTORS Instructor: Prasun Dewan

COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

COMP 401

INHERITANCE – INHERITED

VARIABLES AND CONSTRUCTORS

Instructor: Prasun Dewan

Page 2: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

2

PREREQUISITE

Inheritance

Page 3: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

3

MORE INHERITANCE

Inheritance

Graphics Examples

Inherited Variables

Constructors

Memory Representation

Page 4: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

4

POINT INTERFACE

Read-only properties

defining immutable

object!

public interface Point { public int getX(); public int getY(); public double getAngle(); public double getRadius(); }

Page 5: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

5

CLASS: ACARTESIANPOINT

public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int theX, int theY) { x = theX; y = theY; } public ACartesianPoint(double theRadius, double theAngle) { x = (int) (theRadius*Math.cos(theAngle)); y = (int) (theRadius*Math.sin(theAngle)); } public int getX() { return x; } public int getY() { return y; } public double getAngle() { return Math.atan2(y, x); } public double getRadius() { return Math.sqrt(x*x + y*y); } }

Page 6: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

6

EXTENSION: MUTABLE POINT

public interface MutablePoint extends Point {

void setX(int newVal);

void setY(int newVal);

}

Page 7: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

7

CALLING CONSTRUCTOR IN SUPERCLASS

public class AMutablePoint extends ACartesianPoint

implements MutablePoint {

public AMutablePoint(int theX, int theY) {

super(theX, theY);

}

public void setX(int newVal) {

x = newVal;

}

public void setY(int newVal) {

y = newVal;

}

}

Superclass constructor ‘Inherited” but not available to users of the

class if not called from subclass

Page 8: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

8

BOUNDED POINT

public interface BoundedPoint extends MutablePoint {

public MutablePoint getUpperLeftCorner();

public MutablePoint getLowerRightCorner();

public void setUpperLeftCorner(MutablePoint newVal);

public void setLowerRightCorner(MutablePoint newVal);

}

Page 9: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

9

ADDING VARIABLES

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) {

super(initX, initY);

upperLeftCorner = anUpperLeftCorner; lowerRightCorner = aLowerRightCorner; fixX();

fixY();

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

Superclass constructor

Variables also extended unlike in previous examples

Page 10: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

10

BOUNDED POINT IMPLEMENTATION public void setX(int newVal) {

super.setX(newVal);

fixX();

}

public void setY(int newVal) {

super.setY(newVal);

fixY();

}

public MutablePoint getUpperLeftCorner() {

return upperLeftCorner;

}

public MutablePoint getLowerRightCorner() {

return lowerRightCorner;

}

public void setUpperLeftCorner(MutablePoint newVal) {

upperLeftCorner = newVal;

fixX();

fixY();

}

public void setLowerRightCorner(MutablePoint newVal) {

lowerRightCorner = newVal;

fixX();

fixY();

}

}

Page 11: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

11

NEW CLASS HIERARCHY AND VARIABLES

AMutablePoint

ACartesianPoint

Object

ABoundedPoint

int x int y

MutablePoint

upperLeftCorrner

MutablePoint

lowerRightCorrner

Page 12: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

12

CALLING CONSTRUCTOR IN SUPERCLASS

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) {

super(initX, initY);

upperLeftCorner = anUpperLeftCorner;

lowerRightCorner = aLowerRightCorner;

fixX();

fixY();

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

Super call

Page 13: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

13

CALLING CONSTRUCTOR IN SUPERCLASS

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) {

upperLeftCorner = anUpperLeftCorner; lowerRightCorner = aLowerRightCorner; fixX();

fixY();

super(initX, initY);

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

Super call

Super call must be first statement in constructor but not other methods.

Superclass vars initialized before subclass vars, which can be used by thelatter

Subclass vars not visible in superclass

Subclass may want to override initialization in super class

Page 14: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

14

CALLING CONSTRUCTOR IN SUPERCLASS

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) {

upperLeftCorner = anUpperLeftCorner; lowerRightCorner = aLowerRightCorner; fixX();

fixY();

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

No Super call

Java complains that no super call

Wants Super class variables to be initialized

Page 15: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

15

CALLING CONSTRUCTOR IN SUPERCLASS

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) { x = initX; y = initY; upperLeftCorner = anUpperLeftCorner; lowerRightCorner = aLowerRightCorner; fixX();

fixY();

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

Java complains that no super call

Wants Object variables to be initialized

Manual initialization

Page 16: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

16

MISSING SUPER CALL

public class AStringSet extends AStringDatabase {

public AStringSet () {

}

public void addElement(String element) {

if (member(element)) return;

super.addElement(element);

}

}

No complaints

Page 17: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

17

JAVA INSERTION

public class AStringSet extends AStringDatabase {

public AStringSet () {

super();

}

public void addElement(String element) {

if (member(element)) return;

super.addElement(element);

}

}

Page 18: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

18

MISSING CONSTRUCTOR AND SUPER CALL

public class AStringSet extends AStringDatabase {

public void addElement(String element) {

if (member(element)) return;

super.addElement(element);

}

}

No complaints

Page 19: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

19

JAVA INSERTION

public class AStringSet extends AStringDatabase {

public AStringSet () {

super();

}

public void addElement(String element) {

if (member(element)) return;

super.addElement(element);

}

}

Page 20: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

20

CALLING CONSTRUCTOR IN SUPERCLASS

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) {

upperLeftCorner = anUpperLeftCorner; lowerRightCorner = aLowerRightCorner; fixX();

fixY();

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

No Super call

Could it insert the super call?

Page 21: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

21

CALLING CONSTRUCTOR IN SUPERCLASS

public class ABoundedPoint extends AMutablePoint

implements MutablePoint {

MutablePoint upperLeftCorner, lowerRightCorner;

public ABoundedPoint(int initX, int initY,

MutablePoint anUpperLeftCorner,

MutablePoint aLowerRightCorner) {

super(initX, initY);

upperLeftCorner = anUpperLeftCorner; lowerRightCorner = aLowerRightCorner; fixX();

fixY();

}

void fixX() {

x = Math.max(x, upperLeftCorner.getX());

x = Math.min(x, lowerRightCorner.getX());

}

void fixY() {

y = Math.max(y, upperLeftCorner.getY());

y = Math.min(y, lowerRightCorner.getY());

}

Does not know what the actual parameters should

be

Could initialized them to 0s and null values

There may be multiple constructors, which should

it choose?

Page 22: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

22

MEMORY?

AMutablePoint

ACartesianPoint

Object

ABoundedPoint

int x int y

MutablePoint

upperLeftCorrner

MutablePoint

lowerRightCorrner

Page 23: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

23

INHERITANCE AND MEMORY REPRESENTATION public class ACartesianPoint

implements Point {

int x, y;

}

new ABoundedPoint(75, 75,

new AMutablePoint (50,50),

new AMutablePoint (100,100))

8

16

50

50

ACartesianPoint@8 8

48 ABoundedPoint@48

100 16 ACartesianPoint@16

100

75

75

public class ABoundedPoint

extends AMutablePoint

implements BoundedPoint {

MutablePoint upperLeftCorner ;

MutablePoint lowerRightCorner;

}

Page 24: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

24

INHERITANCE AND MEMORY REPRESENTATION public class ACartesianPoint

implements Point {

int x, y;

}

new ABoundedPoint(75, 75,

new AMutablePoint (50,50),

new AMutablePoint (100,100))

8

16

50

50

ACartesianPoint@8 8

48 ABoundedPoint@48

100 16 ACartesianPoint@16

100

75

75

public class ABoundedPoint

extends AMutablePoint

implements BoundedPoint {

int x, y;

MutablePoint upperLeftCorner ;

MutablePoint lowerRightCorner;

}

0

0

Duplicate variables accessed by subclass methods

Inherited variables accessed by superclass methods

Smalltalk did not allow re-declaration of superclass

variables

When making a super class out of subclass, accidental re-declaration can happen

Page 25: COMP INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS · Inherited Variables ... CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int

25