• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can anyone explain why the following doesnt compile...

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the rule of overridin is that...you cannot override a private or final methoed of the parent class...i think thats the reason....
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic