Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Object References

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a program as follows :

public class A {

private void show() {

System.out.println("A()");
}

public void value() {

this.show();
}

public static void main(String[] args) {

A obj = new B();
obj.value();

}

}

class B extends A {

private void show() {
System.out.println("B()");
}

}

I was expecting the output to be this " B () " , but the output comes out to be : "A ()".I don't understand this . . Now after using "this.show()" I expect the method show belonging to class B to be called instead of the method of the class A , because of the following reasons :

1. show() is a private method in class A , so class B doesn't extend it .
2. I have assigned an object of type B to the reference of class A .
3. I am using the "this" operator with show() , so I expect that the method from class B should be called but show from class A is called instead .

Can anyone please this behavior ?
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bharat bhasin wrote:I wrote a program as follows :

public class A {

private void show() {

System.out.println("A()");
}

public void value() {

this.show();
}

public static void main(String[] args) {

A obj = new B();
obj.value();

}

}

class B extends A {

private void show() {
System.out.println("B()");
}

}

I was expecting the output to be this " B () " , but the output comes out to be : "A ()".I don't understand this . . Now after using "this.show()" I expect the method show belonging to class B to be called instead of the method of the class A , because of the following reasons :

1. show() is a private method in class A , so class B doesn't extend it .
2. I have assigned an object of type B to the reference of class A .
3. I am using the "this" operator with show() , so I expect that the method from class B should be called but show from class A is called instead .

Can anyone please this behavior ?



Firstly, good question !!!

1. show() is a private method in class A , so class B doesn't INHERIT it .
3. Consider another example. Forget the value() method for now. focus on showOff1().




The type of obj is A according to our declaration, but its actually B. So, the JVM checks if there is a method showOff1() in A. Good...then it proceeds to check the actual type of obj, which is B. Do we have a VISIBLE showOff1() in B ?
Yes, so execute the showOff1() of B and not of A.

When you look at show(), the same steps are performed. But, there is a difference.

The 'this' is a reference of type A. The JVM sees there if there is a show() in A. It is there. Then proceed to check 2. Is there a show() in B? We know that there is a show() in A and B.
But the code of class A, ie main method cannot see it because its private. So, the JVM chooses to execute show() of A. <EDIT: Bad choice of words>. The JVM will execute only what is legal/allowed.

Try this - put this code in B and see what happens.





 
bharat bhasin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Andy for your answer .

But I don't understand the explanation .
You say the "this" is a reference of class A , OK I got it .
But I don't understand why it cannot see the show() method of class B . I know it is private and not visible to class A but the JVM checks whether its present in class B and it is present .
I didn't understand how this whole thing works .
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bharat bhasin wrote:Thank you Andy for your answer .

But I don't understand the explanation .
You say the "this" is a reference of class A , OK I got it .
But I don't understand why it cannot see the show() method of class B . I know it is private and not visible to class A but the JVM checks whether its present in class B and it is present .
I didn't understand how this whole thing works .



I did not say that the JVM can see it. YOU and I can see it. But as far as any other class is concerned, it cannot see private methods of B, even if they are in the same package. This is what the keyword private is meant for.
Please read more about access modifiers and such.
    Bookmark Topic Watch Topic
  • New Topic