• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Covariant Return Types Mock from www.cafe4java.com

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"));
}
}

Can someone explain why "SuperCafe4Java" gets printed twice and not "SubCafe4Java"? I understand that the GET method is overloaded and not overriden here, but it still doesn't explain why "SuperCafe4Java" gets printed rather than "SubCafe4Java".
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of overriding, always check object type
and in case of overloading, always check reference type
[ June 17, 2007: Message edited by: Shubha Kirani ]
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers

i think second scenario was obvious , in the first case you are calling the inherited method on SUB-Class reference with the SUPERCLASS Reference, though it's not displayed in the program but actually it's running meaning it's calling the inherited method of the SUB-Class.

- thank you
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shubha Kirani:
In case of overriding, always check object type
and in case of overloading, always check reference type



It is making no sense to me.

It would check the visibility of the method through the object type and invoke the method through the object reference. Get back, if not clear.

However, you can check it by running the program after removing the method from parent and then from child. It wouldn't compile even, and you need make some amendments in your main method. Check out the compiler complains, play with it a little.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kram,
You are right ! They are overloaded methods....
Overloading is compile time polymorphism....So compiler will place the call at compile time only....
Method are called on object refference only(exception is overridind)....So both times you are calling methods on SuperCafe4Java superFoo refference....So both the times SuperCafe4Java 's get() version will be called

Regards
Swapnil
 
Kram Nart
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's clearer now that I've had a chance to play with the code a little bit. The superFoo reference doesn't have access to any of SubCafe4Java's methods at all, including the "public Object get(String o)" at compile time. Polymorphism only kicks in when the methods are overridden.

Thanks everyone.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic