• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Private methods' inheritance

 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This may be a silly thing, but I always assumed that private methods do not get inherited because they are just not visible in the derived class. However, giving the code below:



Everthing compiles fine and when run it I get the following output: base method() on Derived

Is this how it is supposed to happen?

[Edit]
This one is even more suprising for me:


The output is now: base method() on Derived. It looks like it is overloading instead of overriding.
[ July 03, 2007: Message edited by: Sergio Tridente ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method() is visible only in Base class. It runs through a variable of Base type. When method() is called, it prints the name of the classe of the object is currently running (which is an instance of Derived).
 
Rafael Souza
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i replied before you hadn't edited yet.

ok... what is happening here... when you write method() in Derived class you are actually creating a new method... you are not overriding that one in Base class... you can check this by trying to change the return type or even declaring new checked exceptions in class Derived (things that wouldn't be possible if you were overriding Base's method().
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods are not visible by the subclasses. In this case if the derived
class is supposed to have a method with same signature, it is not
overriding of method. It is just another method. More clearly, polymorphism
doesn't apply there. In other words, the derived class version of the method
wont be called using the base class ref variable. This is because you have
not overridden the method, private methods are not visible.


Thanks,
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic