public class Toto {
public static void main(String[] args) {
Toto toto = new Toto();
System.out.print(toto.toString(toto.getMyObj()));
}
MyObj getMyObj(){
return new MySubObj();
}
String toString(MyObj swap){
return "MyObj";
}
String toString(MySubObj swap){
return "MySubObj";
}
}
class MyObj{
}
class MySubObj extends MyObj{
}
public class Toto {
public static void main(String[] args) {
Toto toto = new Toto();
System.out.print(toto.toString(toto.getMyObj()));
}
MyObj getMyObj(){
return new MySubObj();
}
String toString(MyObj swap){
return "MyObj";
}
String toString(MySubObj swap){
return "MySubObj";
}
}
class MyObj{
}
class MySubObj extends MyObj{
}
I think the answer is MyObj.
From the code , the toString method seems overloaded correctly.
Step 1. toto.getMyObj() returns a MyObj object which is actually a MySubObj.
Step 2. What is passed in as the argument of toto.toString()? an object of MyObj type
Step 3. So, the toString(MyObj swap) is called instead of toString(MySubObj)
Step 4 Therefore, it prints MyObj.
Correct me if I am wrong.