• Post Reply Bookmark Topic Watch Topic
  • New Topic

Subclass and Private Field  RSS feed

 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why doesn't this code work?

I get an error on line 5. Why?

Since B extends A then B (even though it does not inherit x) has a copy of x.
x is private, therefore it can only be accessed within its class (in this case A).
The parameter in the second getX() is of type B and is located inside A, then why can't I access x directly?

I appreciate your help...
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch!

Class B has no visibility of private member x in class A. Therefore trying to directly access x from anywhere in class B is not possible.
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Hello, and welcome to the Ranch!

Class B has no visibility of private member x in class A. Therefore trying to directly access x from anywhere in class B is not possible.


Hello. Let's substitute line 9 with:
When executing new B().printX(), then this (in the method's body) should be referring to a B object but this.x works without any problem!
Why?
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because method getX() from class A is public and is visible from class B. Once inside method getX() in class A, 'this' refers to class A so the private member x is 'in scope' and can be read.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again Please indent your code; you are dealing with a PC screen with about 50 lines of text, not some little 2½″ screen on a phone. So there is no need to try to squash a method into one line. I shall try copying your code with correct indentation and you can see how much better it will look
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what it lookslike with Allman indentation:-
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember: private members are not inherited by subclasses. If you have non‑private getXXX and setXXX methods those are inherited by subclasses assuming they are accessible.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize this is just experimental code but you should never have a reference to a subclass in a superclass. That is, your reference to class B on line 5 is a huge red flag that indicates a serious design flaw.
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is experimental. I want to understand the nature of access modifiers and inheritance.

b.x generates error because x, since not inherited, is not a member of B even though a copy of x exists in a B object. Right?
Also, when I try ((A) b).x then the program works fine! Is it because x is a member of A and the return statement is inside the class A?

Also if I have,


Why the error (i.e. x has protected access in beautifulworld.Parent)?
I know that a protected member can be accessed inside its class, other classes in the same package, and subclasses.
What is wrong with my understanding?
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramsin Khoshaba wrote:a copy of x exists in a B object. Right?

Incorrect. x does not exist in B.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm all for learning about the limits of a language and all but I don't see any particular use in the way you're approaching this exploration of scope and visibility in Java. These things you are trying are not things you would do in normal programming situations. It's like trying to see if you can kick your head. Sure, with enough effort you may be able to do it but you have to ask yourself "Why would you want to do that?"
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramsin Khoshaba wrote:Also, when I try ((A) b).x then the program works fine! Is it because x is a member of A and the return statement is inside the class A?

Yes, correct.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!