Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Polymorphism

Polymorphism means "many forms", and it occurs when many classes are related to
each other by inheritance. Polymorphism is derived from 2 Greek words: poly and
morphs. The word "poly" means many and "morphs" means forms. So polymorphism
means many forms.

When attributes and methods from another class are inherited to a class,
Polymorphism uses those methods to perform different tasks. This allows performing
a single action in different ways.

Suppose, a superclass called Animal that has a method called animalSound().


Subclasses of Animal could be Pigs, Cats, Dogs, Birds, etc. - And they also have their
own implementation of animalSound(). When this occurs, polymorphism takes place.

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {


public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {


public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
There are two types of polymorphism in Java:
 Compile-time Polymorphism, and
 Runtime Polymorphism.

Polymorphism in java can be performed by


 Method Overloading (Compile-time Polymorphism), and
 Method Overriding (Runtime Polymorphism).

Runtime Polymorphism or Dynamic Method Dispatch is a process in which a call


to an overridden method is resolved at runtime rather than compile-time.
 In this process, an overridden method is called through the reference
variable of a superclass (Ex. Animal). The determination of the method to
be called is based on the object being referred to by the reference variable
(Ex. myPig, myDog).
 If the reference variable of Parent class refers to the object of Child class, it
is known as Upcasting.

class A{}
class B extends A{}
A a=new B(); //upcasting

For upcasting, the reference variable of class type or an interface type can be used.

interface I{}
class A{}
class B extends A implements I{}

Here, the relationship of class B would be:

B IS-A A
B IS-A I
B IS-A Object // Since Object is the root class of all classes in Java
Runtime Polymorphism with Data Member

A method is overridden, not the data members, so Runtime Polymorphism can't be


achieved by data members.

class Bike {
int speedlimit=90;
}
class Honda3 extends Bike {
int speedlimit=150;

public static void main(String args[]) {


Bike obj=new Honda3();
System.out.println(obj.speedlimit); //90
}

Overriding vs Overloading
Overriding implements Runtime 1. Overloading implements Compile-time
Polymorphism. Polymorphism.
The Method Overriding occurs between 2. Overloading occurs between the
superclass and subclass. methods in the same class.
Overridden Methods have the same 3. Names of Overloaded Method are the
signature i.e. same name and method same but the parameters are different.
arguments.
With Overriding, the method call is With Overloading, the method to call is
determined at the runtime based on the determined at the compile-time.
object type.
If Overriding breaks, it can cause serious If Overloading breaks, the compile-time
issues in the program because the effect error will come and it’s easy to fix.
will be visible at runtime.

****

You might also like