Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Inheritance concepts

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

I still can't clearly understand inheritance in Java.

What I know:
1) Inheritance models the IS-A relationship.
2) An instance of a subclass is also an instance of the superclass.
3) The private variables of a superclass are contained in objects of the subclass.

What confuses me:

1) If private fields of a superclass are contained within the subclass's object, then what is meant by membership in Java? That is, what does it mean to be a member of a class? I'm asking this because private fields are not members of the subclass.

2) How do access modifiers actually work? I've looked through many explanations, but I'm still stuck.



That line of code works, but why?

Thanks.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you google the access modifiers you will see why.
In order of least accessible to most accessible they are private, default(package), protected and public.

private: only the class owning it can see/use it
default: the class, and all other classes in the same package can use it
protected: the class, all other classes in the same package, and all subclasses of the owning class can access it
public: any class can access it

In your example you have a protected attribute
The subclass (in a different package) can thus access it

It couldn't if the access modifier was public or "default"

 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) If private fields of a superclass are contained within the subclass's object, then what is meant by membership in Java? That is, what does it mean to be a member of a class? I'm asking this because private fields are not members of the subclass.



My understanding of "Member" is that it encompasses anything owned by a class. Attributes, methods etc.
Private fields are members of the class they are declared in obviously.
Your question mainly appears to be "is a private field a member of the subclass as well"?

My opinion would be yes. They are actually present in the subclass.
The attribute/method doesn't disappear. It is just not accessible by the subclass.
 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:. . . My understanding of "Member" is that it encompasses anything owned by a class. Attributes, methods etc. . . .

There is a strict definition in the JLS (=Java® Language Specification. Beware: that can be difficult to read). It includes fields methods inner classes etc, but not constructors. Actually, this line seems easy to read:-

The JLS §8.1.16 wrote:A class body may contain declarations of members of the class, that is, fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5).

Members inherited from superclasses are also included.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, from 8.2 (and pertinent to private members):
"Members of a class that are declared private are not inherited by subclasses of that class."
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:private: only the class owning it can see/use it
default: the class, and all other classes in the same package can use it
protected: the class, all other classes in the same package, and all subclasses of the owning class can access it
public: any class can access it



Well, that is what I don't understand in the context of inheritance.

For example, a protected field in Parent is visible in Child (a subclass of Parent).
So a Child object may access the protected field, but why? Because "a protected field in a superclass is accessible in the subclass."
I can't really understand why that reason (in quotation marks) entails the former behavior.

I'm really confused, but the problem is that I can't express it...
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:But, from 8.2 (and pertinent to private members):
"Members of a class that are declared private are not inherited by subclasses of that class."



That's exactly why I asked what is meant by membership in Java.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramsin Khoshaba wrote:
For example, a protected field in Parent is visible in Child (a subclass of Parent).
So a Child object may access the protected field, but why? Because "a protected field in a superclass is accessible in the subclass."
I can't really understand why that reason (in quotation marks) entails the former behavior.



Your example has nothing to do with a subclass class accessing a protected field of the super class. In fact, your subclass doesn't have any code at all.

It is your Test class that is accessing the protected field. And it is allowed to do that because it is in the same package as the super class (where the protected field is declared). As already mentioned, protected access includes package access.

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

Henry Wong wrote:Your example has nothing to do with a subclass class accessing a protected field of the super class. In fact, your subclass doesn't have any code at all.



But I created a subclass object in Test.

Do I have to distinguish between the accessing reference and the accessing class?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramsin Khoshaba wrote:

Dave Tolls wrote:But, from 8.2 (and pertinent to private members):
"Members of a class that are declared private are not inherited by subclasses of that class."



That's exactly why I asked what is meant by membership in Java.



Stefan is correct. Private fields declared in the superclass, are included in instances of the subclass. It is just not accessible from the subclass -- but of course, the subclass can call methods of the superclass, such as a getter method, which can access it.

Henry
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramsin Khoshaba wrote:
But I created a subclass object in Test.

Do I have to distinguish between the accessing reference and the accessing class?



Access permissions are always from the point of view of the executing code (where the executing code is located) trying to access the field (where the field is declared).

It is *never* based on the accessing reference type*.

Henry


* Note: not completely true. There is also a "responsible for implementation" requirement for protected fields, which does depend on the reference type.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramsin Khoshaba wrote:But I created a subclass object in Test.
Do I have to distinguish between the accessing reference and the accessing class?


Yes.

One aspect of Java's "inheritance laws" that I've always found slightly confusing is that protected means "visible to a subclass OR a class in the same package" - and personally, I kind of wish they'd left that second bit out.

However, to answer the specific questions from your original post:
1. Line 6 (o.i) works because i is declared in Parent, which is in the same package as Test. The "red herring" here is that o's type (Child) is not; but that doesn't alter the fact that i is visible to "a[ny] subclass OR a class [in this case Test] in the same package".

2. Line 3 (extends a.Parent) works because a.Parent is public, so anything can see it.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . protected means "visible to a subclass OR a class in the same package" . . .

I have seen no end of confusion about that because it is different from C++

A very long time ago there was private protected which (I think) meant in subclasses only, but that has been withdrawn.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have seen no end of confusion about that because it is different from C++



Hmmm.... On one hand, it does seem like a weird level of access. On the other hand, C++ supports something similar, call friend classes, which is many times more flexible (arguably useful). And IMHO, it is also much more confusing to use, then just the package access of Java.

Henry
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic