Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming in Java

Similar presentations


Presentation on theme: "Advanced Programming in Java"— Presentation transcript:

1 Advanced Programming in Java
Polymorphism Mehdi Einali

2 agenda Polymorphism Polymorphism Application
Polymorphism and Method Call Class Diagram

3 Polymorphism

4 Animals can talk!

5 Talk request You can send a talk request to an animal What does it do?
It depends on type of the animal

6 Musical Instruments The same note
Different sounds on different instruments

7 Polymorphic Behavior Common interface Different behaviors
The same request Different behaviors Depending on the type of object

8 Polymorphism The same interface But different implementation
animal.talk() instrument.play(int note) But different implementation in different classes

9 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;

10 UpCasting Upcasting Upcasting is always valid
Shape s = new Rectangle(); Circle c = new Circle(); Shape s = c; Upcasting is always valid

11 DownCasting Downcasting Shape s = … Circle c = s;
Circle c = (Circle) s; Needs type cast On your own risk May cause errors ClassCastException

12 What About Method Calls?
Shape s = new Rectangle(); s.draw(); double d = s.getArea(); Circle c = new Circle(); Shape s = c;

13

14 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 Private methods Final methods Static methods

15 Run-time Method Binding
Also known as Dynamic Binding When you call a method on a superclass reference Actual method is bound in runtime (If it is overridden) Performance overload

16 Applications of Polymorphism

17 Applications of Polymorphism
Polymorphic behavior Classes : Player, Referee, Ball, … Drawable(superclass)

18 No Polymorphism

19 With Polymorphism

20 Animal Example

21 Cat and dog

22 Polymorphic Animals!

23 Hint on Array Initialization

24 Polymorphism and method call

25 class Animal{ public void f(){ System.out.println("f() in Parent"); } class Cat extends Animal{ System.out.println("f() in Child"); public class SomeClass { public void method(Animal p){ System.out.println("method(Parent)"); public void method(Cat p){ System.out.println("method(Child)");

26 What is the output of: Cat cat= new Cat();
Animal animal= new Animal(); Animal animalCat= new Cat(); animal.f(); cat.f(); animalCat.f(); Output: f() in Parent f() in Child

27 What is the output of: SomeClass caller = new SomeClass();
caller.method(animal); caller.method(cat); caller.method(animalCat); Important Note: Polymorphic behavior for method call Method call evaluated based on content not reference Not for the parameters Parameter evaluated based on reference not content More specific to broader (based on reference!!) Output: method(Parent) method(Child)

28 Note: Overloading method() is overloaded in SomeClass
public class SomeClass { public void method(Parent p){ System.out.println("method(Parent)"); } public void method(Child p){ System.out.println("method(Child)"); method() is overloaded in SomeClass Two independent methods

29 To Overload or to Override
class SomeClass { public void method(Parent p){ System.out.println("method(Parent)"); } class SomeSubClass extends SomeClass{ public void method(Child p){ System.out.println("method(Child)"); method() is overloaded in SomeSubClass It is not overridden Two independent methods

30 What is the output of: SomeSubClass ref = new SomeSubClass();
ref.method(parent); ref.method(child); ref.method(parentRefToChild); Output: method(Parent) method(Child)

31 Output: method(Parent) method(Child) method(Parent) method(Child)

32 Output: method(Parent) method(Parent)

33 Output: Exception : there is no method for [method(animal), method(animalCat)] Exception : there is no method for [method(animal), method(animalCat)]

34 It is about external behavior not internal data or static attributes
Is-a means every cat has animal behavior too, it is not about use every cat instead of animal (such as method param). It is about external behavior not internal data or static attributes What about is-a relationship between cat and animal? What about Polymorphism between cat and animal? Polymorphism means every inherited behavior in animal must evaluated based on its overriden childish behavior! It is not applicable here! What about Liskov substitution principle between cat and animal? Liskov is about changing parent with child and expect same external behavior. It is rephrase of is-a relationship. Output: method(Parent) Exception for barfoo.method

35 It is no applicable here!
foo is a right bar . nothing wrong! It is no applicable here! What about is-a relationship between foo and bar? What about Polymorphism between foo and bar? Polymorphism means every inherited behavior in bar must evaluated based on its overridden childish behavior! What about Liskov substitution principle between foo and bar? Same as is-a relationship. it is not applicable here! There is not overridden method in bar. Two overloaded method in bar and foo. method(Parent) method(Child)

36 A Question When we override equals() method
why do we pass Object as the parameter? For example class Person has an equals method like this: public boolean equals(Object obj) {…} But not like this: public boolean equals(Person obj) {…} Why?!

37 Class diagram

38 UML – Unified Modeling Language
The Unified Modeling Language (UML) is a standard language for specifying, visualizing, constructing and documenting the artifacts of software systems, as well as for business modeling and other non-software systems.

39 Composite Structure Diagram
UML Diagrams Structural Diagrams Class Diagram Object Diagram Component Diagram Composite Structure Diagram Deployment Diagram Package Diagram Behavioral Diagrams State Machine Diagram Use Case Diagram Activity Diagram Interaction Diagrams Timing Diagram Interaction Overview Diagram Communication Diagram Sequence Diagram

40 class A class diagram describes the types of objects in the system and the various kinds of static relationships that exist among them

41 کلاس خوب نام واضح یک تجرید مشخص از یک مولفه از فضای مسئله نگاشت واضح به یک مفهوم مسئولیت های کم و خوش تعریف – کلاس قدر قدرت – ضدالگو سه تا پنج مسئولیت انسجام بالا وابستگی کم

42 Notes on class diagram Classes should be simple, understandable and clear High Cohesion/Low Coupling Don’t mention every thing comprehensiveness is the enemy of comprehensibility! Avoid omnipotent classes

43 End

44 Martin fowler http://martinfowler.com/ ThoughtWorks New methodology
Technology radars 1996 1997 1999 2000 2002 2010 2012


Download ppt "Advanced Programming in Java"

Similar presentations


Ads by Google