aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can Any  Explain This Code... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can Any  Explain This Code..." Watch "Can Any  Explain This Code..." New topic
Author

Can Any Explain This Code...

Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
class SuperCafe4Java {
public Object get (Object o) {
return ("SuperCafe4Java");
}
}

class SubCafe4Java extends SuperCafe4Java {
public Object get (String o) {
return ("SubCafe4Java");
}
}

class TestCafe4Java {
public static void main (String[] arguments) {
SuperCafe4Java superFoo;
SubCafe4Java subFoo;

superFoo = new SubCafe4Java();
System.out.println (superFoo.get("super"));

subFoo = new SubCafe4Java();
superFoo = subFoo;
System.out.println (superFoo.get("super"));
}
}

OutPut:SuperCafe4Java
SuperCafe4Java
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well the output is

SuperCafe4Java
SuperCafe4Java

because get(Object o) is not overridden by get(String o). Instead the two methods are overloaded. So when you call get("super") with a SuperCafe4Java class reference, the compiler looks for a method matching get("super") in the SuperCafe4Java class. It finds the get(Object o) method matching this method (as get(String o) is not available in SuperCafe4Java). So that method is called.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Here get() method of SuperCafe4Java is overloaded in the sub class SubCafe4Java.
Its not overridden.(Overridden method calls will be determined at run time)
And the calls to the overloaded methods will be determined at compile time.

So the get method in SuperCafe4Java will be executed not the one in SubCafe4Java.

See page 109 of K&B 5.0 for more info!

Hope this is clear!


Thanks,<br />Srilatha M
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
But When Im Running This Two Code...Getting Different OutPut




M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
Thank you...My Doubt Has got cleared...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Any Explain This Code...