• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Super Class Concept

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain this code what this code produce the output Sub?


Code
/*

class Super{
}
class Sub extends Super{
}
class TestSuper
{
public void conf(Super sup){
System.out.println("Super");
}

public void conf(Sub sub){
System.out.println("Sub");
}

public static void main (String args[])
{
new TestSuper().conf(null);
}

}
*/

Thanks in Advance
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When there is a function call whose argument matches super classes and also subclasses, JVM try to call the most specific one. Here in case which is "class Sub".

But if you define another version of the same function like the following...

then call to

will result an compiler error. Because both String and (Sub or Super) can hold the null value but String or (Sub and Super) don't has any subclass superclass relation. So JVM cannot determine which function to call, results an ambiguity.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi AtulKumar,

have a look at the following discussion. The example is much more complicated, but it may be helpful.
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Klug
I refferd that link which you have suggested in your post but still the point is not clear .
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Key word here is "most-specific".

Scenario 1:
------------
Suppose we have a Class B which extends Class A :
Object <-- Class A <-- Class B
So here we have above hierarchy.

Suppose now we have three methods:
public void method(Object o);
public void method(A a);
public void method(B b);

and if we call method(null) then it will most specific method i.e method having B as parameter

Scenario 2:
-----------
Suppose we have two independent classes A and B :
Object <-- A
Object <-- B

Suppose now we have three methods:
public void method(Object o);
public void method(A a);
public void method(B b);

and if we call method(null) then it will have two most specific methods i.e method having A as parameter and method having B as parameter and hence will give compilation error.

I hope this is clear to you.

Murali...
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a good blog by Corey on most-specific method.

Murali...
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Murali!!!Thanks
The Blog as well as your exlanation was perfect it helped a lot......
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to All ranchers and specially to Murli
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic