-1

1.Can Some one illustrate how it works? Why it prints 1 instead of 2?

2.From the Oracle Official Java tutorial, the definition of this: Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

    // Base Class
    class A { 

        public int m = 1;


        public void view(){
            // Print variable m
            System.out.println(this.m);

            //print the current class name
            System.out.println(this.getClass());
        }

    }

    // Subclass extends Base Class
    public class B extends A{

        // Define another variable m
        public int m = 2;

        public static void main(String[] args) {
            B b = new B();
            b.view();
        }

    }

Below is the debug of the view(){}:

My Question is: You can see the current object is B, when we usethis, it represents the B, m = 1 in base class A, m = 2 in derived class B, then use the this.m, it should print 2, isn't it?

Reacher
  • 356
  • 4
  • 16
  • 2
    There exists **no** property overriding in java. – Lino Jul 16 '18 at 09:10
  • 2
    Possible duplicate of [Override a property in Java](https://stackoverflow.com/questions/38163255/override-a-property-in-java) – Lino Jul 16 '18 at 09:11
  • 4
    Methods can be overridden, not fields. `B.m` is a different field from `A.m`. – khelwood Jul 16 '18 at 09:11
  • 2
    When the compiler compiles the code for class `A`, it interprets the expression `this.m` to mean the `m` that class `A` defines. It neither knows nor cares that you're going to introduce another class with a different `m`. – Dawood ibn Kareem Jul 16 '18 at 09:19
  • 1
    The comments provide the answer, and that's why high-grade IDE's for java typically have a feature that issues warnings against name shadowing. – Erwin Smout Jul 16 '18 at 09:34
  • @Lino Hi mate, I know there is no fields overriding in java, what I'm asking is that: when `B` object runs this line of code: `System.out.println(this.m);`, `this` means `B`, right ? (because `this`represents the current object), then because of the reason that B has a field `m = 2`, why the answer is not 2? – Reacher Jul 16 '18 at 09:54
  • @khelwood Hi mate, I know there is no fields overriding in java, what I'm asking is that: when `B` object runs this line of code: `System.out.println(this.m);`, `this` means `B`, right ? (because thisrepresents the current object), then because of the reason that B has a field `m = 2` why the answer is not 2? – Reacher Jul 16 '18 at 09:55
  • @DawoodibnKareem But, `this` do represents the current object which is `B`, when runs`this.m`, it means `B.m`, why just simply because `this.m` is in `class A`, so the answer is 1 ? – Reacher Jul 16 '18 at 09:56
  • 1
    When the compiler compiles the method `view`, it knows that `this` will be an instance of `A`, and it resolves `this.m` as the `m` field declared in `A`, which is the only thing it possibly _could_ resolve it to. The fact that your object at run-time has another field also called `m` does not affect that. – khelwood Jul 16 '18 at 10:02
  • @khelwood, thats not right, `this` is an instance of `B`, just copy the code above and run it, this line of code: `this.getClass()` prints `Class B` – Reacher Jul 16 '18 at 10:06
  • 2
    It's _runtime_ type is `B`. But the _compiler_ regards it as type `A`, because it's `this` inside a method in class `A`, and that's how `this` works. – khelwood Jul 16 '18 at 10:09
  • 3
    An object of class `B` has two fields called `m`. Which one `this.m` refers to depends on which class it's written in, because the choice is made at compile time, not run time. – Dawood ibn Kareem Jul 16 '18 at 10:41

1 Answers1

1

Variables are not polymorphic in Java; they do not override one another.

Ankita Mehta
  • 442
  • 3
  • 16