Wednesday, 9 April 2014

Polymorphism

Polymorphism

class A extends Object
{
public String toString()
{
return "A";
}
}
class B extends A
{
public String toString()
{
return "B";
}
}
class C extends B
{
public String toString()
{
return "C";
}

}
public class PolymorphismDemo
{
public static void main(String[] args) {
fun(new C());//invokes the method toString() of class C
fun(new B());//invokes the method toString() of class B

fun(new A());//invokes the method toString() of class A

}
public static void fun(Object x) {
System.out.println(x.toString());
}
}

No comments:

Comment

Comment Box is loading comments...