Java

What is Polymorphism ?

What is Polymorphism ?

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.

Polymorphism is one of the piller of object oriented programing language. The main purpus use of polymorphism one methods in many forms. Method overriding is polymorphism example.

public class A{
public void polyexamp()
{
System.out.println(“Parent Class A”);
}
}

class B extends A{
public void polyexamp()
{
System.out.println(“Child Class B”);
}
}

class C extends A{
public void polyexamp()
{
System.out.println(“Child Class B”);
}
}

public class PolyTest
{
public static void main(String[] args) {
A a1 = new B();
a1.polyexamp(); //Child Class B

Animal a2 = new Dog();
a2.makeNoise(); //Prints Bark
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *