0
class Student {
          
          void cal()
          {
              System.out.println(this);
              calculate(this);
          }

            void calculate(Student s)
           {
               System.out.println("Student");
           }

           void calculate(HelloWorld h)
           {
               System.out.println("HelloWorld");
           }
         
              }


    public class HelloWorld extends Student {
    
        public static void main(String... args) {
    
            HelloWorld karan = new HelloWorld();
            karan.cal();
    
        }
    
        }
    

OUTPUT OF CODE:

HelloWorld@65ab7765

Student

When the compiler comes across the karan.cal(), it first checks if a cal() is present in the Student class. Once confirmed and compiled, the code runs and JVM sees the object type karan is pointing to and thus executes the cal() of the Student class. When the cal() is executed, even though this contains the address for HelloWorld object, the calculate(Student s) is being run.

I know that the JVM doesn't take part in method overloading so it won't decide what calculate() to call.

Does the compiler replace this with some particular value so that even when it is pointing to a child object, the method that contains the parent reference is executed, despite there being a method that can be more suitable to point towards the child object?

  • Your mistake was assuming method args use polymorphism. Java is a single dispatch language. See https://stackoverflow.com/q/9759141/217324 – Nathan Hughes Jan 29 '22 at 11:04
  • 1
    Within the `Student` class, `this` is a variable of type `Student` even if it points to an object of type `HelloWorld`, so it can only resolve to the `calc(Student)` method. – Mark Rotteveel Jan 29 '22 at 11:05
  • @MarkRotteveel Is there a way by which I can print the type of 'this' reference variable? – Karan Guleria Jan 29 '22 at 11:45
  • 1
    @KaranGuleria You can't determine the type of the variable itself, but it is quite simple: for an instance of `HelloWorld` in code *enclosed* in the `Student` class, `this` is of type `Student`, code enclosed in the `HelloWorld` class, `this` is of type `HelloWorld`, and for code enclosed in the `Object` class, this is of type `Object`. However, you can determine the type of the *value* of the variable (e.g. `instanceof`, `getClass`, etc) – Mark Rotteveel Jan 29 '22 at 11:49
  • @MarkRotteveel Okay. When 'this' is used in Object class then it will be of type Object, does the compiler put some extra information next to 'this' keyword so that the JVM knows that 'this' is of type Object. Also, can you recommend a book that teaches such Java internals in a good manner? – Karan Guleria Jan 29 '22 at 11:57
  • 1
    `this` as such only exists in source form, and the compiler will look at the lexically enclosing class to infer its type for purposes of compilation. After compilation, the type of this is no longer relevant. For details, your could read the Java Language Specification and Java Virtual Machine Specification. – Mark Rotteveel Jan 29 '22 at 12:04
  • 1
    There is nothing special about `this`. Just write `Student that = new HelloWorld(); calculate(that);` and you have exactly the same behavior. Just like you would have `Student that = this; calculate(that);`. Try writing `HelloWorld that = this;` and you get a compiler error because `cal()` within class `Student` can not assume `this` to be of type `HelloWorld`. – Holger Jan 31 '22 at 11:54

0 Answers0