File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Strange behaviour of inherited classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange behaviour of inherited classes" Watch "Strange behaviour of inherited classes" New topic
Author

Strange behaviour of inherited classes

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Please see the following code. the output for this code is given against the println statements.
class Base {
public boolean foo(Base b) {
return true;
}
}
class Sub extends Base {
public boolean foo(Sub s) {
return false;
}
}
public class Test {
public static void main(String argv[]) {
Base bb = new Base();
Base bs = new Sub();
Sub ss = new Sub();
System.out.println(bb.foo(bb)); //true
System.out.println(bs.foo(bs)); //true
System.out.println(bs.foo(ss)); //true
System.out.println(bs.foo(bb)); //true
System.out.println(ss.foo(bs)); //true
System.out.println(ss.foo(bb)); //true
System.out.println(ss.foo(ss)); //false
}
}

Also see this code:

class Base {
public boolean foo(int b) {
return true;
}
}
class Sub extends Base {
public boolean foo(int s) {
return false;
}
}
class Test2 {
public static void main(String argv[]) {
Base bb = new Base();
Base bs = new Sub();
Sub ss = new Sub();
System.out.println(bb.foo(10)); //true
System.out.println(bs.foo(10)); //false
System.out.println(bs.foo(10)); //false
System.out.println(bs.foo(10)); //false
System.out.println(ss.foo(10)); //false
System.out.println(ss.foo(10)); //false
System.out.println(ss.foo(10)); //false
}
}
What is interesting is that in the source code given above the values are true(6 times) followed by false and in the code below the o/p is true followed by false(6 times). In the above code how is the resolution done.I have read in many books that the runtime resolution for methods is done based on the object and not on the reference. in that case the first code should give the same o/p as the second one and we can also see that the subclass in first code can be assigned to super class also so it is perfect for the o/p to be as the 2nd one.


Thanks,
Shekar
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Moving to the Beginners forum
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Resolution of polymorphic methods is done at runtime. Resolution of overloaded methods is done at compile-time.
In your first example, the two methods are overloaded. Both methods are available in objects of the child class. The compiler figures out which one to call based on the compile-time type of the variables. The runtime object types are irrelevant.
Polymorphism is when a method in a subclass has an identical signature to a method in a superclass (signature here means method name and exact argument types.) In your second example, there is polymorphism, and so all the method resolution is done at runtime; the object is a Sub, so false is always printed. There's no way for code outside the Sub class to access the overridden method in Base.
See the JavaRanch campfire stories for more info.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange behaviour of inherited classes
 
Similar Threads
Strange behaviour of inherited classes
K&B - Erratum ?
equals() vs inheritance
Overriding or Overloading problem
a question about extends