0% found this document useful (0 votes)
32 views11 pages

Polymorphism

Polymorphism allows objects of different types that share a common superclass to be treated as objects of that superclass. This allows a reference variable of the superclass type to refer to any subclass object. When calling methods, polymorphism allows the correct subclass version of the method to be called based on the actual object type. An array of superclass type can hold subclass objects, and calling a method on each array element will correctly call the specific subclass implementation of that method.

Uploaded by

mohammad rabai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

Polymorphism

Polymorphism allows objects of different types that share a common superclass to be treated as objects of that superclass. This allows a reference variable of the superclass type to refer to any subclass object. When calling methods, polymorphism allows the correct subclass version of the method to be called based on the actual object type. An array of superclass type can hold subclass objects, and calling a method on each array element will correctly call the specific subclass implementation of that method.

Uploaded by

mohammad rabai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

POLYMORPHISM

1 By: Mohammed Jabari


POLYMORPHISM
 Polymorphism works with Inheritance
hierarchies
 Polymorphism literally means: The
occurrence of something in many different forms.
 Recall the Inheritance hierarchy of the shapes classes
 Rectangle is a Shape
 Circle is a Shape
 Square is a Rectangle, thus, we can say
 Square is a Shape
 Now consider the situation when we want to create an array
of Shapes objects.
 The code would look like this:
Shape[ ] shapes = new Shape[4];
 This line of code will create an array of objects of type
Shape.
 Now, what type of objects can we store in the array
“Shapes”?
 Well, any object type of the same family, i.e. from the
same inheritance hierarchy.
 For example, consider the following code:
Shape s1; // s1 variable of type Shape
s1 = new Rectangle(); // s1 is referencing object of type Rectangle
  Doesjava allow me to do that?
 The answer is yes, because Rectangle is a member of the
family (hierarchy)
 Polymorphism allows the assignment of supclass
object references to superclass variables. 2
 Now we return the Shapes Array
shapes[0] = new Rectangle(“Rec1”, 3,4);
shapes[1] = new Circle(“Circle1”, 4); shapes[2]
= new Triangle(“Tri1”, 6, 4); shapes[3] = new
Square(“Square1”, 5);

 All of the above statements are true, because we are


assigning objects of supclasses types to an array
variable of superclass type.
 Consider the following code line shapes[4] = new Shape(“my
shape”);
 This statement is wrong because Shape is an abstract class
and so we can’t create object out of abstract classes.
 Again recall the shapes hierarchy.
2
 Each class of the shapes hierarchy has a method called
area.
 Each class has its own way for implementing the area
method.   Now lets go back to our shapes array and
consider the following code:
shapes[0].area();
shapes[1].area(); shapes[2].area();
shapes[3].area();
 The compiler will allow this kind of statements on one
condition, the condition is that the superclass type
should have an area method
 For the current situation this condition holds as the
superclass, the Shape, has a method called area.
 At run time the system determines which version of
the area( ) method to call depending on the actual 2
object type, so
 shapes[0].area(); // call the area method of the Rectangle
 shapes[1].area(); // call the are method of the Circle
 shapes[2].area(); // call the area method of the Triangle
 shapes[3].area(); // call the area method of the Square

POLYMORPHISM (RECAP)
 Remember, Polymorphism means; the occurrence of
something in many different forms.
 We have called the area method of the Shapes array objects,
and every time different version (form) of the area method
were called.
 Moreover, we deal with objects of types Rectangle, Circle,
Triangle and Square in two different ways. One time we
consider them of the subclass type and another time as object
of superclass type
2
 In particular, Polymorphism allow us to write programs that
process objects that share the same superclass in a class
hierarchy as if they are all objects of the same superclass.
 So, Polymorphism allows to us to program in general rather
than in specific.
ENHANCED FOR LOOP
 To iterate through all the objects of the array shapes we
can use the following code:
for(int i=0; i<shapes.lenght;i++)
System.out.println(shapes[i].area( ));
 We can use the enhanced for loop to accomplish the
same effect
For(Shape s : shapes)
System.out.println(s.area( )); 2
 we read the above statement: for each object s of type
Shape in the collection shapes
 The general format for the Enhanced For:
For(Type variable : collection)
TYPICAL SCENARIO WHERE WE CAN USE
POLYMORPHISM
 Consider as an example a video game that has a number of
objects each of which has a draw method to be drawn on the
screen.
 Suppose also, that each of these object inherits from the same
superclass which contains the draw method.
 we can have one array of all the objects and we can call the
draw method the same way for every object in the game, still,
every object has his own draw method. 2
 Moreover, presenting new object types for the hierarchy
would require minimal changes to the code.
 This style of programming is called Polymorphic
programming.
CASTING AND INSTANCEOF
 Casting is the explicitly conversion from one type to another.
 For example:
double x = 10.5; int
y = (int) x;
 This is called explicit cast as we force the conversion of the
type double to be int.
 The same thing can be done with object types   Example:
Rectangle r1 = (Rectangle) shapes[0];   The above line of
code is only correct if the real type of the array object
shapes[0] is a Rectangle.
2
 If the real type is something else, say for example Circle, that
would result in an exception (runtime error).
CASTING AND INSTANCEOF (CONT..)
 We can check for the real type of the object using the
statement “instanceof”
 Example:
If(shapes[0] instanceof Rectangle){
Rectangle rec1 = (Rectangle) shapes[0]; }
 The if statement will only be true if object shapes[0] real
type is Rectangle.
 Important: In the book “Java How to Program” take a
look at the example at page 469.
SUMMARY OF THE ALLOWED ASSIGNMENTS 2
BETWEEN SUPERCLASS AND SUPCLASS
VARIABLES

1. Assigning a Superclass object reference to a Superclass


variable is straightforward.

Person p1 = new Person();


2. Assigning as Subclass object reference to a Subclass
variable is straightforward.

Rectangle rec1 = new Rectangle(“rectangle1” , 3, 5);


3. Assigning as a Subclass object reference to a Superclass
variable is safe. However, this reference can only be used to
access Superclass members.
2
Shape s1 = new Rectangle(); s1.area(); // will call
the Rectangle version of area.
4. Assigning a Superclass object reference to a Subclass
variable:
 Cause a compiler error.
Shapes s1 = new Rectangle(); // safe, case 3
Rectangle r1 = s1; // cause compilation error   To
avoid the error explicit cast must be used
Rectangle r1 = (Rectangle) s1; // correct if the actual
// type of s1 is Rectangle
 However, if the actual object type at runtime was not a
subclass, an exception occurs.
 “instanceof” can be used to check the actual type if (s1
instanceof Rectangle){
Rectangle r1 = (Rectangle) s1;
} 2

You might also like