GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super Class Concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Super Class Concept" Watch "Super Class Concept" New topic
Author

Super Class Concept

AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
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

Joined: Mar 19, 2007
Posts: 99

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.


MooN
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi AtulKumar,

have a look at the following discussion. The example is much more complicated, but it may be helpful.
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Hi
Klug
I refferd that link which you have suggested in your post but still the point is not clear .
Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
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

Joined: Jul 11, 2007
Posts: 80
Here is a good blog by Corey on most-specific method.

Murali...
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hey Murali!!!Thanks
The Blog as well as your exlanation was perfect it helped a lot......
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Thanks to All ranchers and specially to Murli
 
GeeCON Prague 2014
 
subject: Super Class Concept