aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarify.... 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 "Clarify...." Watch "Clarify...." New topic
Author

Clarify....

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
class Super
{
void method(Super s1)
{
System.out.println("Super");
}
}

class Sub extends Super
{
void method(Sub sb1)
{
System.out.println("Sub");
}
}

class TestInher
{
public static void main(String [] args)
{
Super sp1 = new Super();
Sub sb1 = new Sub();
Super spb = new Sub();

spb.method(sp1); //Produces O/P Super "No Doubt in it"
spb.method(sb1); //Produces O/P Super "Why"
spb.method(spb); //Produces O/P Super "No Doubt in it"
}
}

// I thought that it should print "Sub" for second method call...
// Why it prints "Super".....


$Hi/\bEr
Pinkal Patel
Ranch Hand

Joined: Jun 16, 2006
Posts: 57


Here spb is Super type Reference Variable
So No definition find for as void method(Sub sb1)
But Super is display because you can pass Sun instead of Super...

If you Try above Code it will gave Method not fount Exception.....

Which will help you to understand Concepts.


Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729



[B]Please use meaningful topic titles and use markers[/B]

Super, the type of spb, has a method taking a Super parameter. The type of the actual parameter sb1 is Sub which is a Super by inheritance. So sb1, being also a Super, is applicable to the method in Super which prints "Super".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
I cant understand ...
Plz make it more clear.....
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
When we call method using super class reference and sub type object, the method in the sub class should be called... But, here Why the super class method is called... Plz explain.....
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
When we call method using super class reference and sub type object, the method in the sub class should be called... But, here Why the super class method is called... Plz explain.....
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66


First thing you have to remember is that using a Super referance you can't call methods of Sub ( Except overridden methods ). So here the only method available through spb is


Hence the method of Super class is called and the result follows.


Now see the below code



This is because, now the overridden methods are called.


Let me know if I am wrong.
[ June 29, 2006: Message edited by: Murali Mohan ]

Thanks,<br />Murali...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Shiaber Shaam:
When we call method using super class reference and sub type object, the method in the sub class should be called... But, here Why the super class method is called... Plz explain.....


In your original code there is no overridden method. The method in Sub has a different signature to the method in Super.
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143


In your example, the method defined in the subclass has the same name and return type as a method in superclass but it has different argument type, therefore, it makes it a correct overload of the method declared in the superclass not a correct overidding method. In case of overloaded methods, polymorphism does not play any role and the method call depends on the reference type used to invoke the method(which in your example is always a super class ref.) not on the object stored in the reference.

Hope this helps
Asha


SCJP 1.5
 
 
subject: Clarify....