Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Inheritance

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Khalid Mughal:
All the members of the superclass are inherited by the subclass
The answer given: true
Well,JLS says that
Constructors ,static initializers and instance initializers are not members of the class and therefore not inherited..
But what happened to private members of the superclass?? I thought that they are not inherited but visible in the subclass object.
Please Clarify
Chitra
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private members are neither inherited nor visible in the subclass.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Mughal as well "Private members are not accessible from any other class. This also applies to subclasses, whether they are in the same package or not. It is not to be confused with inheritance of members by the subclass. ~*~ Members are still inherited, but they are not accessible in the subclass. ~*~"
So they are inherited but not accessible, what's the signeficance of that? I'm not sure, maybe it affects if a member in the subclass tries to hide the member in the super class.
Can anyone tell?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alferd you made a very good point. To further your argument:
Let us say that a member method is inherited by sub-class because it is not private and not static. Just it happens that within this method, it access some of the private members of super class. Now you are executing this method in the sub-class as an inherited method. Technically, sub-class does not have direct access to super class private members. But via this method can access them from super class even though it is run on sub class objects.
Hope this makes sense...
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat,
that's exactly how private members are supposed to be accessed.
For more info about private members, please check out:
JLS 6.6.8 Example: private Fields, Methods, and Constructors
JLS 8.2.1.3 Inheritance with private
JLS 8.2 Class Members
 
Politics is a circus designed to distract you from what is really going on. So is 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
    Bookmark Topic Watch Topic
  • New Topic