0% found this document useful (0 votes)
2 views8 pages

Chapter 5-Polymorphism

oop note

Uploaded by

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

Chapter 5-Polymorphism

oop note

Uploaded by

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

Object Oriented Programming

Computer Engineering Department


AASTU
November, 2021
Chapter 5
Polymorphism

2
Polymorphism
Polymorphism is a Greek word meaning “many
forms”.
Polymorphism enables us to “program in the general”
rather than “program in the specific.”
Example: Suppose we create a program that
simulates the movement of several types of animals
for a biological study.
 Classes Fish, Frog and Bird represent the three types of
animals under investigation.
 Imagine that each of these classes extends superclass
Animal, which contains a method move and maintains an
animal’s current location as x-y coordinates.

3
Polymorphism(cont’d)
 To simulate the animals’ movements, the program sends each
object the same message once per second.
 However, each specific type of Animal responds to a move
message in a unique way
 a Fish might swim three feet, a Frog might jump five feet and a
Bird might fly ten feet.
 The program issues the same message (i.e., move ) to each
animal object generically, but each object knows how to
modify its x-y coordinates appropriately for its specific type
of movement.

The same message (in this case, move ) sent to a


variety of objects has “many forms” of results—
hence the term polymorphism.

4
Polymorphism(cont’d)
E.g. 2 One person present in different behaviors:
 Suppose if you are in class room that time you behave like a student,
when you are in market at that time you behave like a customer,
when you at your home at that time you behave like a son or
daughter.

The most common use of polymorphism in OOP occurs


when a parent class reference is used to refer to a child
class object.
The following two output statements will produce different
results, depending on whether p is a Dog or a Cat:
Pet p;
p = new Dog( );
System.out.println(p.speak( ));
p = new Cat( );
5
Example
class Pet {
private String name;
public String getName( ) {
return name;
}
public void setName(String petName) {
name = petName;
}
public String speak( ) {
return "I'm your cuddly little pet.";
}
}
class Cat extends Pet {
public String speak( ) {
return "Don't give me orders.\n" + "I speak only when I want to.";
}
}
6
Example(cont’d)
class Dog extends Pet {
public String fetch( ) {
return "Yes, master. Fetch I will.";
}
}
===============================
public class RunPetDog{
public static void main(String args[]){
Pet p;
p = new Dog( );
System.out.println(p.speak( ));
p = new Cat( );
System.out.println(p.speak( ));

}
7
}
Example(cont’d)
Output
I’m Your cuddly little pet
Don’t give me orders
I speak only when I want

You might also like