Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sadegh Aliakbary Sharif University of Technology Fall 2010.

Similar presentations


Presentation on theme: "Sadegh Aliakbary Sharif University of Technology Fall 2010."— Presentation transcript:

1 Sadegh Aliakbary Sharif University of Technology Fall 2010

2 Agenda Inheritance Initialization in inheritance Polymorphism Fall 2010Sharif University of Technology2

3 Review Inheritance Is-a relationship Protected members Subclass may add, use or override superclass methods Software reuse Bad smell : avoid copy & paste Fall 2010Sharif University of Technology3

4 Review(2) Fall 2010Sharif University of Technology4

5 super keyword Access to parent members Why we need it? The super reference can be used to refer to the parent class often is used to invoke the parent's constructor Fall 2010Sharif University of Technology5

6 Initialization Constructors are not inherited even though they have public visibility We often want to use the parent's constructor to set up the "parent's part" of the object Fall 2010Sharif University of Technology6

7 Constructors A child’s constructor is responsible for calling the parent’s constructor The first line of a child’s constructor should use the super reference to call the parent’s constructor Otherwise, default constructor is implicitly invoked If default constructor does not exist (how?!) You should explicitly call an appropriate parent constructor using super keyword Fall 2010Sharif University of Technology7

8 Practice it… Fall 2010Sharif University of Technology8

9 Variable Scopes Fall 2010Sharif University of Technology9

10 Object class methods equals() toString() finalize() … Fall 2010Sharif University of Technology10

11

12 Polymorphism Suppose Child is a subclass of Parent class. Remember : A Child’s object is also a Parent’s object is-a relationship So these lines are valid: Child c = new Child(); Parent p = new Parent(); p = c; But this line is invalid: c = p; Fall 2010Sharif University of Technology12

13 UpCasting & DownCasting Upcasting Shape s = new Rectangle(); Circle c = new Circle(); Shape s = c; Upcasting is always valid Downcasting Circle c = s; Circle c = (Circle) s; Needs type cast May cause errors Fall 2010Sharif University of Technology13

14 What About Method Calls? Shape s = new Rectangle(); s.draw(); double d = s.getArea(); Circle c = new Circle(); Shape s = c; s.draw(); double d = s.getArea(); Fall 2010Sharif University of Technology14

15 Fall 2010Sharif University of Technology15

16 Compile-time Method Binding Also known as Static Binding When a method is called, compiler knows which method is called The translation is done in compile-time Fall 2010Sharif University of Technology16

17 Run-time Method Binding Also known as Dynamic Binding When you call a method on a subclass object Actual method is bound in runtime (If it is overridden) Performance overload Fall 2010Sharif University of Technology17

18 Applications of Polymorphism Polymorphic behavior Suppose you have so many objects in a GUI application All of them have draw() operation You simply call draw() on every object It knows how to draw itself Classes : Drawable(superclass), Player, Referee, Ball, … Fall 2010Sharif University of Technology18

19 No Polymorphism Fall 2010Sharif University of Technology19

20 With Polymorphism Fall 2010Sharif University of Technology20

21 Animal Example Fall 2010Sharif University of Technology21

22 Cat & Dog Fall 2010Sharif University of Technology22

23 Polymorphic Animals! Fall 2010Sharif University of Technology23

24 Final Methods You can not override final methods final keyword Static method binding for final methods Private methods are implicitly final Static methods are implicitly final Static methods are statically bound Variable is not important Class is important Invoking a static method on an object brings a warning c.getArea()  Circle.getArea() No polymorphism for static variables Fall 2010Sharif University of Technology24

25 Fall 2010Sharif University of Technology25


Download ppt "Sadegh Aliakbary Sharif University of Technology Fall 2010."

Similar presentations


Ads by Google