• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarify....

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".....
 
Pinkal Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



[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".
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant understand ...
Plz make it more clear.....
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic