• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Confusion in inheritance concept

 
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.. Subwe does not inherit private method from the Supwe class, but still an object of Subwe is able to execute the Supwe private method.
I got confused by this. Please help in getting this clarified.
 
Ranch Hand
Posts: 262
4
  • 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.. Subwe does not inherit private method from the Supwe class, but still an object of Subwe is able to execute the Supwe private method.
I got confused by this. Please help in getting this clarified.



It's able to execute the private method? Are you sure? The private method is go(). The access specifier of go1() method is default (i.e when none is explicitly specified).

You might want to refer to Oracle docs for more information.
 
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
When I am printing the current instance (this) executing both the methods i.e. public and private go1() and go(), it is giving the reference of the subclass object created in main method.
I only have one object in the program, the object of subclass and its able to execute private method of super class.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivek Raj wrote:When I am printing the current instance (this) executing both the methods i.e. public and private go1() and go(), it is giving the reference of the subclass object created in main method.
I only have one object in the program, the object of subclass and its able to execute private method of super class.



That IS the expected behaviour.

Oracle docs says the following about the private members in a superclass.

Private Members in a Superclass

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.



Hence you can always access the private members( methods/variables) through the accessible methods of the superclass. Accessible methods include all public and protected methods and if the subclass is in the same package as that of the superclass, they also include the methods without any access specifier ( i.e the methods with default access ).
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic