Tuesday, February 24, 2009

source code to demonstrate covariant returntype in overriding



// sample application to show covariant return types
package oops;

class P
{
public Object m1()
{
System.out.println("super");
return new Object();
}
}

public class OverridingCovariant extends P{

public String m1()
//here m1 different return types valid in 1.5 version
// but return types must parent to child relation.
{
System.out.println("sub");
return "sun";
}
public static void main(String a[])
{
OverridingCovariant s=new OverridingCovariant();
s.m1(); // sub
}
}



0 comments:

Post a Comment