• 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

private vs. public

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found this one recently:


according to the solution the result was:
Parent's method2()
Parent's method1()

and if the first methong was not private but public, protected or default, it would have been:
Parent's method2()
Child's method1()

Can someone help me understanding this? Why is the private method of the Parent class invoked at all?
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods cannot be overridden, they will behave as if they were declared as final.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas De Vos:
private methods cannot be overridden, they will behave as if they were declared as final.



Actually, I don't think that's quite accurate. It is true that a private method can not be overridden - the reason being that a private method is not inherited by the subclass. However, if you declare a method in the subclass with the same signature of a final method in the superclass, you'll get a compilation error. If you declare a method in the subclass with the same signature of a private method in the superclass, you'll get no such error.

Here's a link to an
article I wrote about the private modifier. In addition, here's an excerpt from that article:


A private member method is not inherited by subclasses. Therefore, the method can not be overridden. It can't even be accessed by subclasses. A final member method can not be overridden but it can be accessed from the subclass - it is still inherited. While it causes a compiler error to define a method in a subclass with the same signature of a final method in the superclass, defining a method in the subclass with the same signature as the parent's private method is allowed. Why? The subclass doesn't know about the private method, anyway - it's private to the parent class.

 
Suiram Namuen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your info. I enjoyed to read the comprehensive article about "private".
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to admit I am still confused. Why does Parent's method1() ever execute? Shouldn't it call method1 from the child?
 
Thomas De Vos
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob,

Are you clear that method2() is called from the Parent class?

If so, method1 will be called from within method2 from the Parent class.

But which method1?

Is it method1 from Parent or from the Child class?

Because method1 from Parent is private and hence behaves as final for the Parent class, method1 is executed. For the Child class, method1 does not override method1 correctly from the Parent class and is a method like any other for the compiler and as such does not cause a compilation problem, as Corey explained this would have caused a compilation error when the method was defined as final.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to admit I am still confused. Why does Parent's method1() ever execute? Shouldn't it call method1 from the child?

Here's the way I think of it, Bob. Perhaps this will help you understand what is happening.

First, we have this code:



Pretty simple - we're creating a Child object and assigning it to a Parent reference variable. Next we invoke method2() on that object. Now, what does the JVM do?

First, it looks at the Child class for a method called method2(). Does it find one? Of course not, there's no such method defined. So, next, the JVM looks at the parent class of Child, Parent, for a method called method2(). Sure enough, it finds a matching method and executes it.

Within that method, we find this code:



The println statement is very straightforward. However, the invocation of method1() is the troublesome part. At this point, we are essentially executing within our Parent object. So, the JVM looks for a method called method1() within the Parent class. Sure enough, it finds one. Now, if the method were not declared as private, the JVM would have to check the runtime type of our object and look for an overridden method. But that's not the case. The method is private, therefore it can not be overridden, and no such check is required. The JVM simply executes Parent.method1().

Hopefully, that helps.
 
Bob Law
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thank you very much Corey that makes perfect sense.
 
reply
    Bookmark Topic Watch Topic
  • New Topic