• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super Class Concept

 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • 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.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • 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
  • 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 .
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Here is a good blog by Corey on most-specific method.

Murali...
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to All ranchers and specially to Murli
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic