0% found this document useful (0 votes)
33 views18 pages

Classes: Polymorphism (Part 4) : Sufian, Marini, Norleyza, Noor Faezah

The document discusses polymorphism in Java and how it allows objects of different classes to be treated as the same type if they share a common superclass or interface. It provides an example of an Animal class hierarchy where different animal types like Cat and Tiger override the makeSound() method polymorphically. The AnimalClinic example shows how polymorphism allows the same feedAnimals() method to work on any type of animal object without needing to check the specific class type.

Uploaded by

Nur Ain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views18 pages

Classes: Polymorphism (Part 4) : Sufian, Marini, Norleyza, Noor Faezah

The document discusses polymorphism in Java and how it allows objects of different classes to be treated as the same type if they share a common superclass or interface. It provides an example of an Animal class hierarchy where different animal types like Cat and Tiger override the makeSound() method polymorphically. The AnimalClinic example shows how polymorphism allows the same feedAnimals() method to work on any type of animal object without needing to check the specific class type.

Uploaded by

Nur Ain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Classes :

Polymorphism(Part 4)
sufian, marini,
norleyza, noor faezah
Polymorphism
Java supports polymorphism.
The use of polymorphism can make a
program easier to maintain and extend.
To illustrate the concept of
polymorphism...
Polymorphism

Move!
Polymorphism
‘Poly’ + ‘morph’ + ism  ability to be in
many forms / shapes
An object’s ability to decide what method
to apply itself, depending on where it is in
the inheritance hierarchy
Allows a single variable to refer to objects
from different classes
Polymorphism
Consider the following inheritance hierarchy:

Shape

void
display()

Rectangle Pentagon Triangle

void void void


display() display() display()

Square IsoscelesTriangle EquilateralTriangle


Polymorphism
class Shape {

public void display( ) { }

… The Shape class defines a


} display() method.

class Rectangle extends Shape {


public void display( ) {




}
}
Method display() is
overridden in the
class Square extends Rectangle {
… Rectangle class.
}
class Triangle extends Shape {

public void display( ) {

}

} Method display() is
overridden in the
class EquilateralTriangle extends Triangle {

Triangle class.
}

class IsoscelesTriangle extends Triangle {



} Method display() is
overridden in the
class Pentagon extends Shape { Pentagon class.

public void display( ) {

}

}
An example of applying polymorphism is shown below:

POLYMORPHISM The
display() method
class Application {
executed depends on
public static void main(String[ ] args) { the actual type of the
Shape shapes[] = new Shape[50]; receiver object.
shapes[0] = new Square(...);
shapes[1] = new IsoscelesTriangle(...);
shapes[2] = new Pentagon(...);
for (int i=0; i < 3; i++)
shapes[i].display();
}
}

 Programmer need not test the actual type of


the receiver object to determine the method to
be executed.
 This results in simpler code.
Polymorphism
The display( ) method that will be executed for
shapes[i].display() depends on the actual type of the
object referred to by shapes[i]

For example..
class Rectangle {

public void display( ) { :Rectangle
… shapes
} display()
… :EquilateralTriangle
} display()

class Pentagon { :Square display()



public void display( ) {
… display()
:Pentagon
}
… display()
} class Triangle { :Square

public void display( ) {

}

}
Polymorphism
An object’s ability to decide what method
to apply itself, depending on where it is in
the inheritance hierarchy
What if we wish to extend the program to
include hexagons?
We just extend the inheritance hierarchy by adding a
new subclass for hexagons.

Shape

void
display()

Rectangle Pentagon Triangle Hexagon

void void void void


display() display() display() display()

Square IsoscelesTriangle EquilateralTriangle


Polymorphism

class Hexagon extends Shape {



public void display( ) {

}

}
Polymorphism
With Java’s support for polymorphism, the for loop in
the main() method DO NOT NEED to be modified.
Thank you,
polymorphism!
class Application {
There is no need to
public static void main(String[ ] args) {
Shape shapes[] = new Shape[50]; modify this statement
shapes[0] = new Square(...); in order to take into
account Hexagon
shapes[1] = new IsoscelesTriangle(...);
shapes[2] = new Pentagon(...); objects.
for (int i=0; i < 3; i++)
shapes[i].display();
}
}
Animal Clinic Example
Animal
String name
boolean hungry
Animal(Sring name)
void introduceSelf()
void eat()
String getName()
String makeSound()

Cat Tiger

Cat(String name) Tiger(String name)


void introduceSelf() String makeSound()
String makeSound()
class Animal {
private String name;
private boolean hungry;

public Animal(String n) {
name = n;
hungry = true;
}

public void introduceSelf() {


System.out.print(name);
if (hungry)
System.out.print(" (hungry)");
else
System.out.print(" (full)");
System.out.println();
}

public void eat() {


System.out.println(name+": Thank you! Mmmm...“ +
makeSound());
hungry = false;
}
...
}
class AnimalClinic {

private Animal[] cage;


public AnimalClinic(int numCages) {
cage = new Animal[numCages];
for (int i=0; i < numCages; i++)
cage[i] = null;
}

public void listAnimals() {


for (int i=0; i < cage.length; i++)
if (!isEmpty(i))
cage[i].introduceSelf();
}
POLYMORPHISM
public void feedAnimals() {
System.out.println("================");
System.out.println(“MEAL TIME!!!!!");
System.out.println("================");
for (int i=0; i < cage.length; i++)
if (!isEmpty(i))
cage[i].eat();
}
......
}
class MyAnimalClinic {
public static void main(String[] args) {
AnimalClinic clinic = new AnimalClinic(30);
clinic.register(new Cat("Along"));
clinic.register(new Cat("Rambo"));
clinic.feedAnimals();
clinic.register(new Cat("KiKi"));
clinic.register(new Tiger(“Hobbes"));
clinic.feedAnimals();

clinic.listAnimals();
}
}

Compile and run the program.


Study the output to understand the program.

You might also like