Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

about Inheritance

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TRUE or FALSE
All the mumbers of superclass are inherited by its subclass.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
False
only non private members of superclass are inherited by its
subclass.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that subclasses inherit ALL members of their superclass, however they cannot directly access members declared as private. These members becomes hidden to the very class which owns them...triiiiicky!
Barb
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barbara: I can't agree with you

Originally posted by Barbara Foute Nelong:
I thought that subclasses inherit ALL members of their superclass, however they cannot directly access members declared as private. These members becomes hidden to the very class which owns them...triiiiicky!
Barb


According to JLS 8.2, private members are not inherited by the subclass.
 
Barbara Foute Nelong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edy,
I think this is a pretty confusing one. I gave it some thought.
If we are talking merely about accessibility and we say that all member of a class are accessible to that class, being the private members of the superclass NOT ACCESSIBLE to the instance of the subclass you can say that the subclass does not inherit superclass' private members.
However this does not means that when you create an instance of the subclass memory is not allocated for the private members of the superclass. In fact every instance of a subclass contains an instance of the superclass. That is a subclass has all its data + all the data defined in the superclass. Only it cannot directly access the data defined as private.
Isn't it a matter of terminology? Yes, but if it is how would you answer such a theorical question during the exam? (no problem arise in a code context, because whether you consider the private fields not inherited or inherited but not accessible the outcome is the same)
Cheers
Barb
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a theoretical question.
For example, constructors are not inherited by the subclasses. Private members are inherited, you just can't use them.
My question is: do you consider constructors members?
 
Barbara Foute Nelong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mhhhh, constructors are not inherited. Every class must define its own constructors. A subclass EXTENDS a superclass, which means that adds some data and\or behavior to the superclass'. The object that get instantiated (through the call to the constructor) has all the data of the superclass (implicit call to super()) and the new defined data.
To inherit a superclass constructor and use it as is, wold not make much sense because you will end up with an instance of the superclass, not and instance of the subclass.
No, I would say constructors are not inheridet, but the implementation of the class is inherited. An instance of a subclass contain a fully fledged instance of its superclass thanks to the implicit (or explicit) call to the superclass constructor.

Originally posted by Adrian Yan:
This is a theoretical question.
For example, constructors are not inherited by the subclasses. Private members are inherited, you just can't use them.
My question is: do you consider constructors members?


 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is FALSE.
The word ALL is too generic and hence does not include exceptions.
Ajith
 
Barbara Foute Nelong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this put a period to it. Thanks sheriff.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I'd add a note to explain where some of the confusion on this issue is from. Mughal book states that private members are inherited, but their errata strikes that statement as it is contrary to JLS (as already noted in thread).
Mughal book: A Programmer's Guide to Java Certification by Khalid Azim Mughal, Rolf Rasmussen
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is TRUE:
Quote from "Thinking In Java, 2nd Ed", page 40:
"When you inherit from an existing type, you create a new type. This new type contains not only all the members of the existing type (although the private ones are hidden away and inaccessible)..."
 
Barbara Foute Nelong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As stated before...it is a matter of terminology.
you can say
1.subclasses do not inherit private members of superclass (because a class can access all its member, the fact that it cannot access the private members of a superclass makes it look like those are not inherited)
or
2.subclasses inherit all instance fields\methods(NOT CONSTRUCTORS) of their superclass, but can only access those not declared private
HOWEVER
I agree with Ajith ALL members is too generic(it is inclusive of constructors) and the answer should be FALSE.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Adrian Yan:
The answer is TRUE:
Quote from "Thinking In Java, 2nd Ed", page 40:
"When you inherit from an existing type, you create a new type. This new type contains not only ALL the members of the existing type (although the private ones are hidden away and inaccessible)..."


 
What a stench! Central nervous system shutting down. Save yourself 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