This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Object References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object References" Watch "Object References" New topic
Author

Object References

bharat bhasin
Greenhorn

Joined: Jul 19, 2009
Posts: 19
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 ?
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
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.






Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
bharat bhasin
Greenhorn

Joined: Jul 19, 2009
Posts: 19
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

Joined: Nov 22, 2012
Posts: 257
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object References
 
Similar Threads
A small doubt while calling overriden method
Object creation..?
Calling correct methods
Regarding Local Inner Classes
ArrayList in Object Reference Confusion