• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritance concepts

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


In the above code doStuff() is private in Super. But main method is in the class Super and the ref variable 's' is also of type Super, so compiler sees this as perfect method invocation.
However as per K&B instance method invocation is virtual and happens at run-time based on the actual object the ref variable is holding (Sub object in this case) and Sub has not inherited the private method from Super.

But still its calling the private method and printing "In Super". How is this happening?
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivek Raj wrote:

In the above code doStuff() is private in Super. But main method is in the class Super and the ref variable 's' is also of type Super, so compiler sees this as perfect method invocation.
However as per K&B instance method invocation is virtual and happens at run-time based on the actual object the ref variable is holding (Sub object in this case) and Sub has not inherited the private method from Super.

But still its calling the private method and printing "In Super". How is this happening?



Dynamic call to a method only occur when the methods are overridden. Methods with access modifier private can never be overidden. In your code, the method invocation does not follow the dynamic invocation rule of method override, hence the type of the reference variable i.e Super decide which method signature will be invoked, in your case, it is the doStuff() in Super class.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private methods never overridden as Sidharth mentioned. I have tried to modified your example, run and see what happens,
 
Vivek Raj
Ranch Hand
Posts: 33
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its printing
In Super
In Super

A new thing for me. Thank you for modifying the code to make it more tricky.

This means that private Super methods can be called on Sub object, provided the calling method is in super class? Am I correct?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivek Raj wrote:
A new thing for me. Thank you for modifying the code to make it more tricky.


You are welcome.

Vivek Raj wrote:
This means that private Super methods can be called on Sub object, provided the calling method is in super class? Am I correct?


Private method cannot be called directly from object of subclass since it can't be overridden. In the example, public printSuper method has been called of base class which implicitly invoke the private method. I hope it helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic