Poly - many
Morph - Form (behaviour)
Polymorphism is the capability of a method to do different things based on the object.
class polydemo
{
public static void main(String args[])
{
A obj=new A();
obj.show(5);
B obj1=new B();
obj1.show();
}
}
class A
{
//Overloading - Early binding - Static binding - Compile Time
public void show()
{
System.out.println("HELLO");
}
public void show(int i)
{
System.out.println(i);
}
public void show(double i)
{
System.out.println(i);
}
}
class B extends A
{
//Overriding -- late binding -- dynamic binding -- run time
public void show()
{
super.show(); // it will call show method of the superclass
System.out.println("WORLD");
}
}
Morph - Form (behaviour)
Polymorphism is the capability of a method to do different things based on the object.
class polydemo
{
public static void main(String args[])
{
A obj=new A();
obj.show(5);
B obj1=new B();
obj1.show();
}
}
class A
{
//Overloading - Early binding - Static binding - Compile Time
public void show()
{
System.out.println("HELLO");
}
public void show(int i)
{
System.out.println(i);
}
public void show(double i)
{
System.out.println(i);
}
}
class B extends A
{
//Overriding -- late binding -- dynamic binding -- run time
public void show()
{
super.show(); // it will call show method of the superclass
System.out.println("WORLD");
}
}
No comments:
Post a Comment