1 / 38

Java Polymorphism

Java Polymorphism. What we have learned. Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?. What we have learned. Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?

bailey
Download Presentation

Java Polymorphism

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. Java Polymorphism

  2. What we have learned • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. • What are the other two?

  3. What we have learned • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. • What are the other two? • Encapsulation • Inheritance

  4. What we have learned • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. • Why do we need polymorphism? • To achieve software reuse • Facilitate software maintenance and upgrading

  5. What we have learned • Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. • What are the possible forms of polymorphism in C++? • virtual functions (related to type auto-conversion) • templates

  6. Recall… • What does polymorphism mean?

  7. Recall… • What does polymorphism mean? • It refers to the ability to associate many meanings to one method name by means of a special mechanism known as late binding or dynamic binding. • What does late binding mean?

  8. Recall… • What does polymorphism mean? • It refers to the ability to associate many meanings to one method name by means of a special mechanism known as late binding or dynamic binding. • What does late binding mean? • determining the instance function based on the type of the calling object during run time!

  9. Recall… • Inheritance allows us to define a base class and to define software for the base class. That software can then be used not only for objects of the base class but also for objects of any class derived from the base class. • Polymorphism allows us to make changes in the method definition for the derived classes and to have those changes apply to the software written in the base class.

  10. Recall… • Inheritance allows us to define a base class and to define software for the base class. That software can then be used not only for objects of the base class but also for objects of any class derived from the base class. • Polymorphism allows us to make changes in the method definition for the derived classes and to have those changes apply to the software written in the base class. • This is because Java (also C++) permits an object of a subclass type to be treated as an object of any superclass type! This is known as upcasting! It is done automatically!

  11. How did we do that in C++? Do you still recall the Figure example from C++?

  12. How did we do that in C++? Do you still recall the Figure example from C++? Let us assume we have a Figure class and some classes derived from it as shown in the right class diagram.

  13. How did we do that in C++? Do you still recall the Figure example from C++? Let us assume we have a Figure class and some classes derived from it as shown in the right class diagram. There is an external function (or a user) that wants to use the draw() function to draw these different shapes. void fun(Figure *fpt, float ncx, float ncy) { fpt->set_center(ncx, ncy); fpt->draw(); }

  14. How did we do that in C++? Do you still recall the Figure example from C++? Let us assume we have a Figure class and some classes derived from it as shown in the right class diagram. Our goal To facilitate the future maintenance, we want to keep the interface of this function unchanged despite the new inclusion of more derived classes. void fun(Figure *fpt, float ncx, float ncy) { fpt->set_center(ncx, ncy); fpt->draw(); }

  15. How did we do that in C++? class Circle : public Figure { public: Circle(float cx=0, float cy=0, float r=0) : Figure(cx, cy)),radius(r) {} virtual void draw() {new implementation for Circle} …… private: float radius; }; The solution in C++? virtual functions! class Figure { public: Figure (float cx=0, float cy=0) {} virtualvoid draw(){…} void Center(){…} private: float center_x, center_y; }; class Rectangle: public Figure { public: Rectangle(float cx=0, float cy=0, float w=0, float h=0) : Figure(cx, cy) {width=w; height=h;} virtual void draw() {new implementation for Rectangle} …… private: float radius; };

  16. How did we do that in C++? class Circle : public Figure { public: Circle(float cx=0, float cy=0, float r=0) : Figure(cx, cy)),radius(r) {} virtual void draw() {new implementation for Circle} …… private: float radius; }; The solution in C++? virtual functions! class Figure { public: Figure (float cx=0, float cy=0) {} virtualvoid draw(){…} void Center(){…} private: float center_x, center_y; }; class Rectangle: public Figure { public: Rectangle(float cx=0, float cy=0, float w=0, float h=0) : Figure(cx, cy) {width=w; height=h;} virtual void draw() {new implementation for Rectangle} …… private: float radius; }; void fun(Figure *fpt, float ncx, float ncy) { fpt->set_center(ncx, ncy); fpt->draw(); } Late binding!

  17. What about Java? Do we have the same mechanism in Java?

  18. What about Java? Do we have the same mechanism in Java? Yes and No

  19. What about Java? Do we have the same mechanism in Java? Yes and No “Yes” means that Java also supports polymorphism “No” says that it is not achieved using virtual function!

  20. What about Java? Do we have the same mechanism in Java? Yes and No “Yes” means that Java also supports polymorphism “No” says that it is not achieved using virtual function! Particularly, it is achieved automatically without special treatment!

  21. What about Java? Do we have the same mechanism in Java? Yes and No “Yes” means that Java also supports polymorphism “No” says that it is not achieved using virtual function! Particularly, it is achieved automatically without special treatment! Java uses late binding for all the methods except…… let us put the except… part aside at this moment

  22. Recall Example • public class Base • { • int x, y = 0; • public Base(intx_val, inty_val) • { • x = x_val; y = y_val; • System.out.println(“Base class.”); • } • public Base() • { this (0, 0); • } • …… • } • public class Try • { • public static void main(String[] args) • { • Base b_obj = new Base(); • Derived d_obj = new Derived(); • print(b_obj); • print(d_obj); • } • public static void print(Base obj) • { • System.out.println(“x=“+obj.x); • System.out.println(“y=“+obj.y); • } • } • public class Derived extends Base • { • private double x, y = 0; • public Derived() • { • this (0.0, 1.0); • } • public Derived(double x_val, double y_val) • { • super ((int)x_val, (int)y_val); • x = x_val; y = y_val; • System.out.println(“Derived class.”); • } • …… • } Base class. Base class. Derived class. x=0 y=0 x=0 y=1

  23. Recall Example • public class Base • { • int x, y = 0; • public Base(intx_val, inty_val) • { • x = x_val; y = y_val; • System.out.println(“Base class.”); • } • public Base() • { this (0, 0); • } • …… • } • public class Try • { • public static void main(String[] args) • { • Base b_obj = new Base(); • Derived d_obj = new Derived(); • print(b_obj); • print(d_obj); • } • public static void print(Base obj) • { • System.out.println(“x=“+obj.x); • System.out.println(“y=“+obj.y); • } • } • public class Derived extends Base • { • private double x, y = 0; • public Derived() • { • this (0.0, 1.0); • } • public Derived(double x_val, double y_val) • { • super ((int)x_val, (int)y_val); • x = x_val; y = y_val; • System.out.println(“Derived class.”); • } • …… • } Base class. Base class. Derived class. x=0 y=0 x=0 y=1 How to achieve output? …… x=0.0 y=1.0

  24. Example • public class Base • { • int x, y = 0; • public Base(intx_val, inty_val) • { • x = x_val; y = y_val; • System.out.println(“Base class.”); • } • public Base() • { this (0, 0); • } • public void print() • { • System.out.println(“x=“+x); • System.out.println(“y=“+y); • } • } • public class Derived extends Base • { • private double x, y = 0; • public Derived() • { • this (0.0, 1.0); • } • public Derived(double x_val, double y_val) • { • super ((int)x_val, (int)y_val); • x = x_val; y = y_val; • System.out.println(“Derived class.”); • } • public void print() • { • System.out.println(“x=“+x); • System.out.println(“y=“+y); • } • } • public class TryDemo • { • public static void main(String[] args) • { • Base b_obj = new Base(); • Derived d_obj = new Derived(); • b_obj.print(); • d_obj.print(); • } • } We know this is going to work!

  25. Example • public class Base • { • int x, y = 0; • public Base(intx_val, inty_val) • { • x = x_val; y = y_val; • System.out.println(“Base class.”); • } • public Base() • { this (0, 0); • } • public void print() • { • System.out.println(“x=“+x); • System.out.println(“y=“+y); • } • } • public class Derived extends Base • { • private double x, y = 0; • public Derived() • { • this (0.0, 1.0); • } • public Derived(double x_val, double y_val) • { • super ((int)x_val, (int)y_val); • x = x_val; y = y_val; • System.out.println(“Derived class.”); • } • public void print() • { • System.out.println(“x=“+x); • System.out.println(“y=“+y); • } • } • public class TryDemo • { • public static void main(String[] args) • { • Base b_obj = new Base(); • Derived d_obj = new Derived(); • b_obj.print(); • d_obj.print(); • } • } Can we achieve that using polymorphism?

  26. Example • public class Base • { • int x, y = 0; • public Base(intx_val, inty_val) • { • x = x_val; y = y_val; • System.out.println(“Base class.”); • } • public Base() • { this (0, 0); • } • public void print() • { • System.out.println(“x=“+x); • System.out.println(“y=“+y); • } • } • public class Derived extends Base • { • private double x, y = 0; • public Derived() • { • this (0.0, 1.0); • } • public Derived(double x_val, double y_val) • { • super ((int)x_val, (int)y_val); • x = x_val; y = y_val; • System.out.println(“Derived class.”); • } • public void print() • { • System.out.println(“x=“+x); • System.out.println(“y=“+y); • } • } • public class TryDemo • { • public static void main(String[] args) • { • Base b_obj = new Base(); • Derived d_obj = new Derived(); • print(b_obj); • print(d_obj); • } • public static void print(Base obj) • { obj.print(); } • } Yes, see the left.

  27. Let us go back to the Figure example public class Figure { public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public void draw() {System.out.println(“A figure.”); } public void area() {System.out.println(“No define.”); } public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0; }; public class TestFigure { public TestFigure() { } public static void main(String[] args) { Figure f1 = new Square (0, 0, 1); Figure f2 = new Figure (3, 4); Square s1 = new Square (2.3, 3.5, 3); Center(f1, 1, 1); getArea(f1); Center(f2,3,4); getArea (f2); Center(f3,-5, -4); getArea (f3); } public void static Center(Figure obj, float cx, float cy) { obj.reset_center(cx,cy); obj.draw(); } public void static getArea(Figure obj) { obj.area(); } } public class Square extends Figure { public Square(double cx, double cy, double size) { super(); this.size=size;} public void draw() { System.out.println(“A Square.”); } public double area() { return size*size; } private double size=0; }; What are the issues of this code?

  28. Let us go back to the Figure example public class Figure { public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public void draw() {System.out.println(“A figure.”); } public void area() {System.out.println(“No define.”); } public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0; }; public class TestFigure { public TestFigure() { } public static void main(String[] args) { Figure f1 = new Square (0, 0, 1); Figure f2 = new Figure (3, 4); Square s1 = new Square (2.3, 3.5, 3); Center(f1, 1, 1); getArea(f1); Center(f2,3,4); getArea (f2); Center(f3,-5, -4); getArea (f3); } public void static Center(Figure obj, float cx, float cy) { obj.reset_center(cx,cy); obj.draw(); } public void static getArea(Figure obj) { obj.area(); } } public class Square extends Figure { public Square(double cx, double cy, double size) { super(); this.size=size;} public void draw() { System.out.println(“A Square.”); } public double area() { return size*size; } private double size=0; }; What are the issues of this code?

  29. Let us go back to the Figure example public class Figure { public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public void area() {System.out.println(“No define.”); } public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0; }; public class TestFigure { public TestFigure() { } public static void main(String[] args) { Figure f1 = new Square (0, 0, 1); Figure f2 = new Figure (3, 4); Square f3 = new Square (2.3, 3.5, 3); Center(f1, 1, 1); getArea(f1); Center(f2,3,4); getArea (f2); Center(f3,-5, -4); getArea (f3); } public static void Center(Figure obj, float cx, float cy) { obj.reset_center(cx,cy); obj.draw(); } public static void getArea(Figure obj) { obj.area(); } } public class Square extends Figure { public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0; }; So, what will be the output?

  30. Polymorphism in Java Java uses late binding for all the methods except…… Now let us look at the except part. What are the methods that the late binding is not applied?

  31. Polymorphism in Java Java uses late binding for all the methods except…… Now let us look at the except part. What are the methods that the late binding is not applied? • They are two types of these methods • static methods • final methods In both cases, early binding is applied.

  32. Abstract Class Similar to C++, abstract classes are also allowed in Java. So what does an abstract class?

  33. Abstract Class Similar to C++, abstract classes are also allowed in Java. So what does an abstract class? It allows us to postpone the definition of certain methods in a base class. For example, in the previous Figure example, both draw() and area() functions in the Figure class need not have a definition as they only make sense when a more specific shape is provided, e.g. a circle or a square.

  34. Abstract Class To postpone the definition of a method, it is specified as an abstract method. public abstract class Figure { public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public abstract void draw(); public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0; }; Abstract methods define the interface of the base class and all its derived classes, because… A class that has at least one abstract method is called an abstract class. An abstract class must have the “abstract” modifier in its class heading

  35. Abstract Class • An abstract class can have any number of abstract and/or fully defined methods • If a derived class of an abstract class adds to or does not define all of the abstract methods, then it is abstract also, and must add abstractto its modifier • A class that has no abstract methods is called a concrete class

  36. Example • public abstract class Base • { • protected int type = 0; • public Base(inttype) • { type = type; • System.out.println(“Base class.”); • } • public Base() • { this (0); } • public abstract intgetType(); • public String toString() • { • return Integer.toString(getType()); • } • } • public class Derived2 extends Derived • { • private intderived_type = 2; • public Derived2 (inttype) • { super (type); derived_type=type; • System.out.println(“Derived class 2.”); • } • public Derived2() • { this(2);} • public intgetType() • { System.out.print(“Derived class 2 type ”); • return type; • } • } public class TestDemo { public static void main(String[] args) { Base obj1 = new Derived(0,1); Base obj2 = new Base(); Base obj3 = new Derived2(2, 3) System.out.println(obj1); System.out.println(obj2); System.out.println(obj3); } } • public class Derived extends Base • { • private intderived_type = 1; • public Derived (inttype) • { super (type); derived_type=type; • System.out.println(“Derived class.”); • } • public Derived() • { this(1); } • public intgetType() • { System.out.print(“Derived class type ”); • return derived_type; • } • } What are the issues?

  37. Example • public abstract class Base • { • protected int type = 0; • public Base(inttype) • { type = type; • System.out.println(“Base class.”); • } • public Base() • { this (0); } • public abstract intgetType(); • public String toString() • { • return Integer.toString(getType()); • } • } • public class Derived2 extends Derived • { • private intderived_type = 2; • public Derived2 (inttype) • { super (type); derived_type=type; • System.out.println(“Derived class 2.”); • } • public Derived2() • { this(2);} • public intgetType() • { System.out.print(“Derived class 2 type ”); • return type; • } • } public class TestDemo { public static void main(String[] args) { Base obj1 = new Derived(1); Base obj3 = new Derived2(3) System.out.println(obj1); System.out.println(obj3); } } • public class Derived extends Base • { • private intderived_type = 1; • public Derived (inttype) • { super (type); derived_type=type; • System.out.println(“Derived class.”); • } • public Derived() • { this(1); } • public intgetType() • { System.out.print(“Derived class type ”); • return derived_type; • } • }

  38. Example • public abstract class Base • { • protected int type = 0; • public Base(inttype) • { type = type; • System.out.println(“Base class.”); • } • public Base() • { this (0); } • public abstract intgetType(); • public String toString() • { • return Integer.toString(getType()); • } • } • public class Derived2 extends Derived • { • private intderived_type = 2; • public Derived2 (inttype) • { super (type); derived_type=type; • System.out.println(“Derived class 2.”); • } • public Derived2() • { this(2);} • public intgetType() • { System.out.print(“Derived class 2 type ”); • return type; • } • } public class TestDemo { public static void main(String[] args) { Base obj1 = new Derived(0,1); Base obj3 = new Derived2(2, 3) System.out.println(obj1); System.out.println(obj3); } } • public class Derived extends Base • { • private intderived_type = 1; • public Derived (inttype) • { super (type); derived_type=type; • System.out.println(“Derived class.”); • } • public Derived() • { this(1); } • public intgetType() • { System.out.print(“Derived class type ”); • return derived_type; • } • } So, what will be the output?

More Related