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

Subclass and Private Field

 
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...
 
Sheriff
Posts: 4870
317
IntelliJ IDE Python Java Linux
  • 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
Sheriff
Posts: 4870
317
IntelliJ IDE Python Java Linux
  • 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.
 
Marshal
Posts: 69806
277
  • 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: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what it lookslike with Allman indentation:-
 
Campbell Ritchie
Marshal
Posts: 69806
277
  • 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.
 
Sheriff
Posts: 15779
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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
Sheriff
Posts: 4870
317
IntelliJ IDE Python Java Linux
  • 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: 15779
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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?"
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic