public class MyMainClass {
public static void main (String[] args) {
MySuperClass msup=new MySuperClass();
MySubClass msub=new MySubClass();
//msup.a=10;
msub.a=5;
//System.out.println(msup.a +" "+ msub.a);
WithMethod wm=new WithMethod();
MySuperClass msc=new MySuperClass();
msc=wm.theMethod(msub);
System.out.println("Returned value : " + msc.a);
}
}
class MySuperClass {
int a;
}
class MySubClass {
int a;
}
class WithMethod {
public MySuperClass theMethod(MySubClass mscl) {
mscl.a+=9;
return mscl;
}
}
Fails with the error :
C:\Documents and Settings\Administrator\Desktop\Java Programs>javac MyMainClass.
java
MyMainClass.java:26: incompatible types
found : MySubClass
required: MySuperClass
return mscl;
^
1 error
why is covariant return not working ???