[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".
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 ]
Joined: Aug 03, 2002
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.
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.