Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Programmer Certification (OCPJP)
Overloaded method
Anthony Karta
Ranch Hand
Posts: 342
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
why lines #1 and #2 are calling SuperCafe4Java get() method?
the actual object is SubCafe4Java and it has more specific get(
String
) method.
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(new String("super")));//#! subFoo = new SubCafe4Java(); System.out.println (subFoo.get("sub")); superFoo = subFoo; System.out.println (superFoo.get("super")); //#2 } }
SCJP 5
Manju Devarla
Ranch Hand
Posts: 85
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In this program get() method is overloaded so
SuperCafe4Java superFoo = new SubCafe4Java();
System.out.println (superFoo.get(new String("super")));//#!
#1 : superFoo.get() method is called based on the reference not on runtime object type.
Output of this program is
SuperCafe4Java
SubCafe4Java
SuperCafe4Java
NDP Prasad
Ranch Hand
Posts: 177
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If you change the object to String you will get the desired output.
class SuperCafe4Java {public Object get ([B]String[/B] o) {return ("SuperCafe4Java");}}
SCJP,SCWCD<br />
Anuragk kushwaha
Ranch Hand
Posts: 51
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Dear NDP..
waht you did ? , just overriding.
with overriding we can achieve runtime
polymorphism
while using overloading we can achieve compile time polymorphism.
it means in case of overloading method called using
refeneces.
---please correct me if i am wrong.
I found some pretty shells, some sea glass and this lovely tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
covariant data types and overloaded method
Covariant Return Types Mock from www.cafe4java.com
Can Any Explain This Code...
method overriding
covariant doubt
More...