Polymorphism in Java Programming

Polymorphism in Java Programming

Previous Home Next

 

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon.
 In other words, polymorphism allows you define one interface and have multiple implementation. This is one of the basic principles of object oriented

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon .This is the third basic principle of object oriented programming.Overloading, overriding and dynamic method binding are three types of polymorphism.

Overloading

Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list.

Overriding

Overridden methods are methods that are redefined within an inherited or subclass.
They have the same signature and the subclass definition is used.

dynamic method binding


is the ability of a program to resolve references to subclass methods at runtime.
 


 
class Animal
{
void Voice()
{
System.out.println("I am a generic Animal.");
}
}
class Dog extends Animal
{
void Voice()
{
System.out.println("I am barking.");
}
}
class Lion extends Animal
{
void Voice()
{
System.out.println("I am Roaring.");
}
}
class RuntimePolymorphism
{

public static void main(String[] args)
{
Animal ref1 = new Animal();
Animal ref2 = new Dog();
Animal ref3 = new Cow();

ref1.Voice();
ref2.Voice();
ref3.Voice();

}
}

I am generic animal
I am barking
I am Roaring.
Previous Home Next