an implicit argument besides actual method arguments, which is a reference by which the method was called. Consider the following code:
``` Parent p = new Child();
int a = 5;
String b = “b”;
p.someMethod(a, b) ```
Actually, for the JVM , a method invocation looks like this:
``` Parent p = new Child();
int a = 5;
String b = “b”;
p.someMethod(a, b, p) ```
Where p is an impilict parameter for method someMethod.
getClass() method is not an exception, it is also passed an implicit parameter. Moreover, getClass() method uses an implicit parameter to resolve class of the object.
But what if we call this method without specifying object reference explicitly, just getClass()? We can do it, because getClass() is inherited from Object, so we can use it in any instance context in classes we define. Well, after passing a hidden parameter to the following method
``` getClass() ```
In fact a call looks like this
``` getClass(this) ```
Because ‘this’ is a reference to object for which the method was called. It does not matter whether a method is called with super keyword, like this:
``` super.getClass() ```
Because in point of fact it is translated to
``` super.getClass(this) ```
And ‘super’ keyword does not affect ‘this’ reference value. ‘This’ keywords references the Child class object regardless of presence a ‘super’ keyword.
That’s why you get a child class object instead of parent one (although you’ve specified ‘super’ keyword).
Please format the code you posted, by wrapping it in triple backticks. -> `
While the described mechanism is correct (except that this ref is not last, but always the first implicit argument of method call, it's first pushed to stack to be precise), it has nothing about how inheritance works, how virtual method call is dispatched. It has nothing about the original question, to say simple. Or maybe I'm totally misunderstood the original question))) And there's one great mistake I've found in your further description: « Because ‘this’ is a reference to object for which the method was called. It does not matter whether a method is called with super keyword, like this: super.getClass() Because in point of fact it is translated to super.getClass(this) It really matters whether the method is called using`super` keyword!!! And whether this is passed makes no any difference how the method call is resolved. Using super results in virtual method invocation being resolved by parent's vitrual method table. In case of getClass it behaves always the same way - because it is defined as final in java.lang.Object that is base class for all other classes. Thus call getClass() or super.getClass() is always resolved to it's single possible parent implementation - java.lang.Object.getClass(). Lets pretend that getClass is not final in java.lang.Object and it can be overridden. If Child would have it's implementation of getClass() and Parent would have it's own one, and Object also had it's one, then calling getClass() or super.getClass() in Child would matter.
Обсуждают сегодня