Похожие чаты

When JVM invokes any instance method it passes a method

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).

3 ответов

17 просмотров

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.

JVM call this method as SomeMethod(p,a,b); Nd more ovr I can check the parent class using reflection API as this.getCass().getSuperClass();

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта