Advanced Programming in Java Sadegh Aliakbary Sharif University
Advanced Programming in Java Sadegh Aliakbary Sharif University of Technology Fall 2010
Agenda �Inheritance �Initialization in inheritance �Polymorphism Fall 2010 Sharif University of Technology 2
Review �Inheritance �Is-a relationship �Protected members �Subclass may add, use or override superclass methods �Software reuse �Bad smell : avoid copy & paste Fall 2010 Sharif University of Technology 3
Review(2) Fall 2010 Sharif University of Technology 4
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 2010 Sharif University of Technology 5
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 2010 Sharif University of Technology 6
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 Fall 2010 super keyword Sharif University of Technology 7
Practice it… Fall 2010 Sharif University of Technology 8
Variable Scopes Fall 2010 Sharif University of Technology 9
Object class methods �equals() �to. String() �finalize() �… Fall 2010 Sharif University of Technology 10
Polymorphism
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 2010 Sharif University of Technology 12
Up. Casting & Down. Casting �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 2010 Sharif University of Technology 13
What About Method Calls? �Shape s = new Rectangle(); �s. draw(); �double d = s. get. Area(); �Circle c = new Circle(); �Shape s = c; �s. draw(); �double d = s. get. Area(); Fall 2010 Sharif University of Technology 14
Fall 2010 Sharif University of Technology 15
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 2010 Sharif University of Technology 16
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 2010 Sharif University of Technology 17
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 2010 Sharif University of Technology 18
No Polymorphism Fall 2010 Sharif University of Technology 19
With Polymorphism Fall 2010 Sharif University of Technology 20
Animal Example Fall 2010 Sharif University of Technology 21
Cat & Dog Fall 2010 Sharif University of Technology 22
Polymorphic Animals! Fall 2010 Sharif University of Technology 23
Final Methods �You can not override final methods �final keyword �Static method binding for final methods �Private 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. get. Area() Circle. get. Area() �No polymorphism for static variables Fall 2010 Sharif University of Technology 24
Fall 2010 Sharif University of Technology 25
- Slides: 25