• Post Reply Bookmark Topic Watch Topic
  • New Topic

how come method is accessed from subclass here  RSS feed

 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm not able to comprehend what's going on here. Please see the following 2 scenarios. I'm sure I'll be able to think better after a reply.

1. Why is it that the method that is executed depends on the original class to which the object belongs, and not on the class to which casting is done ?. The output is : test() from B


If we are able to call method of subclass from a superclass object, then shouldn't we be able to access int j; from subclass using the superclass object as System.out.println(obj.j)

2.It becomes even more confusing when the two test() methods are declared as static. As in, if you do something as(see code below), then the output becomes : test() from A
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot override static methods. They are typed statically (which is why they are called static); if the compiler sees static method x() in class Foo, then the Foo version is used, irrespective of what the runtime type is.
Non‑final and non‑private instance methods can be overridden, and are therefore polymorphic. Nothing else is polymorphic in Java®. They are bound dynamically (=not statically), so the version of the method in the runtime type's Class object is used.The answer to the confusion is:
Always call static members by the name of the class, not the name of the obect.
 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You cannot override static methods. They are typed statically (which is why they are called static); if the compiler sees static method x() in class Foo, then the Foo version is used, irrespective of what the runtime type is.

Thanks for the explanation Ritchie. Made things much simpler and completely clears the second question from my original post.

However, I need some more help regarding the first question which basically deals with Polymorphism and Over-riding.
I understanding that Polymorphism(slightly toned to fit the context of this question) is the property by which different methods may be called depending on the object through which the call was made, and I understand that obj.test(); will call the respective class A and class B implementations of void test(){} when the objects are homogeneous, as in A obj1 = new A(); and B obj = new B();
But when it comes to something like A obj = new B(); I have a little bit of problem understanding whether the method will be called from class B or class A.

Argument in favor of class B
A obj = new B(); - So basically it's an object of class B, and so the class B implementation will be called

Argument in favor of class A
Though it's an object of class B, it's "auto upcast" to Type class A. So just like this object won't be able to access the countless methods that B has and A doesn't, similarly, it cannot access the test() implementation of class A.

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aftab Hassan wrote:. . . Thanks for the explanation Ritchie. Made things much simpler and completely clears the second question from my original post. . . .
You're welcome Good to see people understanding things.

No, there is no upcasting. That would be static binding to methods. You can design a language to work like that, but Java® was designed differently. For non‑private non‑final instance methods, which I told you yesterday are the only things that can be overridden, Java® uses dynamic binding, probably better called runtime binding. That doesn't apply to static methods, nor to fields, and you can get no end of dangerous confusion if you try to override fields or static methods. It is much easier to understand if you apply it to concrete examples...Do you see the problem with the case on line 12? Only cats have the angry noise method. The cast is dangerously error‑prone and may cause Exceptions. So you could say you are bending the rules of inheritance, that you have a method which requires a cast for it to be used.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!