1 / 25

Comp 401 Inheritance – Inherited Variables and Constructors

Comp 401 Inheritance – Inherited Variables and Constructors. Instructor: Prasun Dewan. Prerequisite. Inheritance. More Inheritance. Inheritance Graphics Examples Inherited Variables Constructors Memory Representation. Point Interface. public interface Point { public int getX ();

mercury
Download Presentation

Comp 401 Inheritance – Inherited Variables and Constructors

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. Comp 401Inheritance – Inherited Variables and Constructors Instructor: Prasun Dewan

  2. Prerequisite • Inheritance

  3. More Inheritance • Inheritance • Graphics Examples • Inherited Variables • Constructors • Memory Representation

  4. Point Interface publicinterface Point { publicintgetX(); publicintgetY(); publicdoublegetAngle(); publicdoublegetRadius(); } Read-only properties defining immutable object!

  5. Class: ACartesianPoint publicclassACartesianPointimplements Point { intx, y; publicACartesianPoint(inttheX, inttheY) { x= theX; y= theY; } publicACartesianPoint(doubletheRadius, doubletheAngle) { x= (int) (theRadius*Math.cos(theAngle)); y= (int) (theRadius*Math.sin(theAngle)); } publicintgetX() { returnx; } publicintgetY() { returny; } publicdoublegetAngle() { returnMath.atan2(y, x); } publicdoublegetRadius() { returnMath.sqrt(x*x + y*y); } }

  6. Extension: Mutable Point publicinterfaceMutablePointextends Point { voidsetX(intnewVal); voidsetY(intnewVal); }

  7. Calling Constructor in Superclass publicclassAMutablePointextendsACartesianPoint implementsMutablePoint{ publicAMutablePoint(inttheX, inttheY) { super(theX, theY); } publicvoidsetX(intnewVal) { x= newVal; } publicvoidsetY(intnewVal) { y= newVal; } } Superclass constructor ‘Inherited” but not available to users of the class if not called from subclass

  8. Bounded Point publicinterfaceBoundedPointextendsMutablePoint { publicMutablePointgetUpperLeftCorner(); publicMutablePointgetLowerRightCorner(); publicvoidsetUpperLeftCorner(MutablePointnewVal); publicvoidsetLowerRightCorner(MutablePointnewVal); }

  9. Adding Variables publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { super(initX, initY); upperLeftCorner = anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { y= Math.max(y, upperLeftCorner.getY()); y= Math.min(y, lowerRightCorner.getY()); } Variables also extended unlike in previous examples Superclass constructor

  10. Bounded Point Implementation publicvoidsetX(intnewVal) { super.setX(newVal); fixX(); } publicvoidsetY(intnewVal) { super.setY(newVal); fixY(); } publicMutablePointgetUpperLeftCorner() { returnupperLeftCorner; } publicMutablePointgetLowerRightCorner() { returnlowerRightCorner; } publicvoidsetUpperLeftCorner(MutablePointnewVal) { upperLeftCorner= newVal; fixX(); fixY(); } publicvoidsetLowerRightCorner(MutablePointnewVal) { lowerRightCorner= newVal; fixX(); fixY(); } }

  11. New Class Hierarchy and Variables Object ACartesianPoint int y int x AMutablePoint ABoundedPoint MutablePointupperLeftCorrner MutablePointlowerRightCorrner

  12. Calling Constructor in Superclass publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { super(initX, initY); upperLeftCorner= anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { y= Math.max(y, upperLeftCorner.getY()); y= Math.min(y, lowerRightCorner.getY()); } Super call

  13. Calling Constructor in Superclass publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { upperLeftCorner = anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); super(initX, initY); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { 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. Subclass may want to override initialization in super class Superclass vars initialized before subclass vars, which can be used by thelatter Subclass vars not visible in superclass

  14. Calling Constructor in Superclass publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { upperLeftCorner = anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { 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

  15. Calling Constructor in Superclass publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { x = initX; y = initY; upperLeftCorner= anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { y= Math.max(y, upperLeftCorner.getY()); y= Math.min(y, lowerRightCorner.getY()); } Manual initialization Java complains that no super call Wants Object variables to be initialized

  16. Missing Super Call public class AStringSetextendsAStringDatabase { public AStringSet () { } publicvoidaddElement(String element) { if (member(element)) return; super.addElement(element); } } No complaints

  17. Java Insertion public class AStringSetextendsAStringDatabase { public AStringSet () { super(); } publicvoidaddElement(String element) { if (member(element)) return; super.addElement(element); } }

  18. Missing Constructor and Super Call public class AStringSetextendsAStringDatabase { publicvoidaddElement(String element) { if (member(element)) return; super.addElement(element); } } No complaints

  19. Java Insertion public class AStringSetextendsAStringDatabase { public AStringSet () { super(); } publicvoidaddElement(String element) { if (member(element)) return; super.addElement(element); } }

  20. Calling Constructor in Superclass publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { upperLeftCorner = anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { y= Math.max(y, upperLeftCorner.getY()); y= Math.min(y, lowerRightCorner.getY()); } No Super call Could it insert the super call?

  21. Calling Constructor in Superclass publicclassABoundedPointextendsAMutablePoint implementsMutablePoint { MutablePointupperLeftCorner, lowerRightCorner; publicABoundedPoint(intinitX, intinitY, MutablePointanUpperLeftCorner, MutablePointaLowerRightCorner) { super(initX, initY); upperLeftCorner = anUpperLeftCorner; lowerRightCorner= aLowerRightCorner; fixX(); fixY(); } voidfixX() { x= Math.max(x, upperLeftCorner.getX()); x= Math.min(x, lowerRightCorner.getX()); } voidfixY() { 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?

  22. Memory? Object ACartesianPoint int y int x AMutablePoint ABoundedPoint MutablePointupperLeftCorrner MutablePointlowerRightCorrner

  23. Inheritance and Memory Representation publicclassACartesianPointimplements Point { int x, y; … } 8 50 ACartesianPoint@8 50 16 100 ACartesianPoint@16 100 publicclassABoundedPointextendsAMutablePointimplementsBoundedPoint{ MutablePointupperLeftCorner ; MutablePointlowerRightCorner; … } 75 48 ABoundedPoint@48 75 8 16 new ABoundedPoint(75, 75, new AMutablePoint(50,50), new AMutablePoint(100,100))

  24. Inheritance and Memory Representation publicclassACartesianPointimplements Point { int x, y; … } 8 50 ACartesianPoint@8 50 16 100 ACartesianPoint@16 100 publicclassABoundedPointextendsAMutablePointimplementsBoundedPoint { int x, y; MutablePointupperLeftCorner ; MutablePointlowerRightCorner; … } 75 48 ABoundedPoint@48 Duplicate variables accessed by subclass methods 75 8 Inherited variables accessed by superclass methods 16 0 Smalltalk did not allow re-declaration of superclass variables 0 new ABoundedPoint(75, 75, new AMutablePoint(50,50), new AMutablePoint(100,100)) When making a super class out of subclass, accidental re-declaration can happen

More Related