• 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

Don't understand why?

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
This question seems quite difficult to understand for me.

OUTPUT: 2
I understand here the method f() is not overridden because it is private. Method g() is available to class sub because of inheritance and this method is calling method f() of Tester class. But I don�t understand exactly why? Then I though may because of I am creating Instance of sub class in Tester class and private member are visible to their class only and f() is private method.
I tried one more thing with little change and output was same �2�.

Please help me.
vivek
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just trying my best to explain based on my observation so please correctly if I'm wrong.
Please note these points:
1) Method overriding occurs only if the method from the base class is inherited to the subclass (thus, the private modifier would prevent method overriding from happening)
2) When a method uses instance variables, the instance variables from the same class as the method will be used
Refer to the code in the 1st post:
- With new Sub().g(), the inherited g() method in the base class is used
- g() returns an int value by calling f(), and this is done within base class
- f() is declared private in the base class, so the JVM thinks: "oh, good, no method overriding here since it's a private method, so I don't even have to bother with checking to see if f() is defined in Sub class or not. I'll just use f() in base class"
- f() in base class is called and 2 is returned
Yeah I'm basically repeating what Vivek said, and Vivek I agree with you that this doesn't quite make sense because in this case the private method f() is actually visible (albeit through the inherited g() method) in the subclass.
So here's my question: Is there a valid reason to allow a private method to be visible to a subclass by means of other inherited methods?
2nd question, since an instance variable can be returned by the private method in the base class the, (and I've even tried returning a private variable through this private method and it worked!) Is this an intended hole in the language to allow us to access private variables? (note that the private method in the base class has to return the variable in order for us to get them through this 'hole')
If not, I wonder if I'd encounter this kind of question in my coming exam.

[This message has been edited by Ken Lai (edited August 10, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken,
Is there a valid reason to allow a private method to be visible to a subclass by means of other inherited methods?
May be, may be not. It is purely an implementation issue which is driven by your requirements. The code in question( or such similar design ) does not violate or contradict any OO principles or language specs.
We all know that private methods or private entities are not visible outside the class. Usually we use them to achieve encapsulation. Eventhough the term encapsulation is used in connection with data, it doesnot preclude encapsulating private methods. ie., providing public interface to private methods. If you are familiar with design patterns, this is one way to implement the facade pattern.
Lets consider a real life situation. You have a class with a private getData() method which reads/computes some internal data. Then there is a public formatData() method which first calls getData() and then formats the data retrieved according to some specifications. If you want the way the data is formatted, you can still subclass this class and override the formatData(). Also years(!) later if you change the way the data is retrieved in getData(), it will not affect the programs/clients who use the formatData() because the public interface remains the same. You will thank yourself for providing an encapsulation of getData().
My example may not be the perfect one, but I hope it gives you something to think about.
Ajith
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
If you remove the private modifier, it will print 1.
Like Ken explained, private methods, static methods, and instance variables do not take part in polymorphism.
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic