Похожие чаты

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 ответов

8 просмотров

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();

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

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

Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
61
Хотел бы спросить у знающих, правильную ли я выбрал книгу для начала изучения ассемблера Юрова В.И ? Или есть более лучшие книги для начала обучения?
Botsman
25
Добрый день, не подскажите, если в OC-V3 поменять страндартную директорию /storage/ на /storage2/ - не будет сильно много проблем ?
Max Dubovsky
32
Conversation at a festival with a non-crpto person (not a normie by any stretch, though): * person: tell me about crypto, me: ok, the original idea is p2p sound money for the...
molecular#123 🐓
25
Книга Юрова В.И пойдёт для обучения?
Botsman
24
$params = [ 'formid' => 'feedbackForm', 'formTpl' => '@CODE: <form class="form-validate" data-id="ajax_form"> <fieldset class="margin-bottom-md"> ...
Pathologic
1
> Примечательно, что новый владелец удаляет из GitHub любые жалобы, указывающие на подозрительную активность или смену владельца, и, видимо, рассчитывает на то, что пользовате...
Alex Sherbakov
2
Today, DENT achieved a significant step forward in DENTX adoption 🥳 Getting DENTX and starting staking is much easier: DENTX is available on Uniswap! Everyone can swap their ...
Thomas
3
I bought some PHA tokens on Kucoin, which is the best wallet to store and easy to use?
AMR
19
Hey there Which is the best Linux destro for developers (coding)? To my research on reddit, they said Linux mint is good for mid level spec and Ubuntu for high Lev hardwar...
Wiz 🪄
11
Карта сайта