Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming (OOP) LAB # 9

Similar presentations


Presentation on theme: "Object Oriented Programming (OOP) LAB # 9"— Presentation transcript:

1 Object Oriented Programming (OOP) LAB # 9
TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

2 Polymorphism

3 Polymorphism Generally, polymorphism refers to the ability to appear in many forms. Polymorphism in a Java program: The ability of a reference variable to change behavior according to what object instance it is holding. This allows multiple objects of different subclasses to be treated as objects of a single super class, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to.

4 Polymorphism An object reference can refer to an object of its class, or to an object of any class related to it by inheritance. For example, if the Shape class is used to derive a class called Square, then a Shape reference could be used to point to a Square object. Shape Square Shape object= new Square();

5 Polymorphism Example#1
Let’s consider we have the Shapes class from the previous lecture.

6 Polymorphism Example#1
Now, we have two subclasses that extend the super class Shape. They are Square and Triangle.

7 Polymorphism Example#1
Notice the object reference changes its value or take more forms. First, it refers to Square object “s”. Second, it refers to the Triangle object “T”. In each time, it calls the right methods. See the result.

8 Polymorphism Example#2
Let’s have a super class called Peron and two subclasses extend it. They are Male and Female classes.

9 Polymorphism Example#2

10 Polymorphism Example#2
Polymorphism allows you to Create an array of two or more objects and loop through them. The process will be more difficult without polymorphism.

11 The End !!


Download ppt "Object Oriented Programming (OOP) LAB # 9"

Similar presentations


Ads by Google