wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can anyone explain why the following doesnt compile... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can anyone explain why the following doesnt compile..." Watch "Can anyone explain why the following doesnt compile..." New topic
Author

Can anyone explain why the following doesnt compile...

Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
This code doesnt compile.


I realise that the print() method in ClassA is private... but xxxis and instance of class B - and class B has also a method called print...

im confused--- is it because Im declaring xxx as type of Class A? can anyone help clarify this.


MV
xu yin
Greenhorn

Joined: Aug 11, 2006
Posts: 2
Because the print() in class A is private. So it is not visiable for xxx.
And xxx is an instance of A, so it can't use the print() in Class B.
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by Firman Drage:
xxxis and instance of class B - and class B has also a method called print...


For compiler xxx is variable of class A. And nothing more.

The information, that this variable actually holds a reference to B instanse will be available only at runtime. And it will be available for VM, not for COMPILER.


SCJP 5.0 - 95%<br />Preparing for SCWCD
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Even I got confused, I always assumed that methods follow runtime binding and variables are bound at compile time. This assumption is correct but there is a caveat.

At compile time the most specific method signature visible from the DECLARED class of variable xxx is choosen to bind - the choice set includes all visible inherited methods. (In this case the A's print method is not visible in class B, since it is private)

In Java the symbolic references are bound at compile time. To know what happens under the hood, read this.


You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Hi,
xxxx is of type A . So first it will check whether print() method is in class A before overriding the print method in class B. If its not present in A , it will give a compile error. The same thing happens here . Because print method in A is not visible to B , it assumes there is no print method in A and gives an error.
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
the rule of overridin is that...you cannot override a private or final methoed of the parent class...i think thats the reason....
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
Completely agree with Shilpa. The JLS says that you cannot override a private method of a class.

When you declare another print() method in the child class, this print just happens to have the same name as a method in the parent class. It is NOT overriding the private method. Hence, the JVM will not dynamically pick the print() method of the child class at runtime.

And of course, the private print method is not visible to the child class, even through a reference to the parent. Hence, a compile-time error is thrown.


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by Aniket Patil:


You are right in general, but in this concrete case that's not the point.
Remove "private" access modifier of method "print" of class A and you will get the same compiler error.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
Removing the private modifier for print method in class A and compiling it again does not thrown any compile-time error. The program works fine.
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by Aniket Patil:
Removing the private modifier for print method in class A and compiling it again does not thrown any compile-time error. The program works fine.



Ooops sorry, it was a bad idea. Now the things are even more complicated;
You can run this method, because this "A.print" has default access, and our B is in the same package.

So please do the following:

1. move your A class to different package,
2. add "public" access modifier to contructor of A.
3. add "protected" access modifier to "A.print",
4. add "public" access modifier "B.print",

Then you will find out, that this problem has nothing to do with overriding.






[ August 16, 2006: Message edited by: Ivan Rebrov ]
[ August 16, 2006: Message edited by: Ivan Rebrov ]
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
First thanks for the reply...
but let say i remove the private method in class A so it looks like this



when compiled and run it will print the value for "i",
the question is -- isnt this the same as when the print method is private...?
i mean, because when a method or variable that belongs to superclass is private the subclass doesnt see it, as if it doesnt exists. and the print method in Class B is simply just a method that belongs to class B... and not an override of the method in Class A.

[ August 16, 2006: Message edited by: Firman Drage CORRECT ME IF IM WRONG ]

i think the solution is... (looking back to the original code.. -see orginial thread) when the reference variable xxx is created its of a type A and at compile time, the xxx.print() will look for the print() method in class A, and since the print method in class A is private... it naturally say that it cannot find it... but let say, for argument sake, that the method A.print() is public.

At "compile time", xxx.print() sees that the method exist because its public -- also remember that is saying that xxx is a holder type A but its content is B -- so xxx.print() at "runtime" it will run the print() in B
[ August 16, 2006: Message edited by: Firman Drage ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anyone explain why the following doesnt compile...
 
Similar Threads
Please teach me...
Certification Study Guide by Simon Roberts Philip Heller Micheal Ernst
SCJP Chapter 3 overloading question
Real Brainer
PRIVATE......