• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New Doubt: from SCJP6.0 K&B MockExam-1

 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just read a Topic on a Doubt from SCJP6.0 K&B Mock Exam

I think that Line 12 should give a compile Error "numbers has private access in Numbers"


Although negate() function is inside Numbers class, the negatives object is a new one.
And numbers.add(-n) is a direct access, which should be allowed only within methods of numbers object, and not this object.

The following code gives compile error, as private member numbers is accessed directly:


 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just checked in C++, it behaves the same way

This code compiles properly:


But this one gives compiler error 'int Numbers::data' is private within this context


Can someone please explain how the access modifiers work in this case (in Java).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private means in Java, that private members can be accessed in the same Class, not from other class. This is way, you got Error in your codes. You are accessing a private member from another class. Private isn't private for same class instances in Java, that's the previous code works.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhimaran for the explanation. So, Private members are private to the class and not the specific objects.
This is one way using which objects of same class can access private members of each other.

After this, I wrote some test codes for checking protected access modifier.
Protected members can be directly accessed through objects of Base class from the derived classes within the same package.
But it doesn't work with subclasses in different package, where one needs to use Derived class object to access base class protected members.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trivikram Kamat wrote:
But it doesn't work with subclasses in different package, where one needs to use Derived class object to access base class protected members.

Could you please post the code here?
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider Class ClassA from package A:


And class ClassB from package B:


Compiling ClassB gives compile time error data has protected access in A.ClassA

But derived class ClassC from package A doesn't give the error:


This is because protected members are accessible in kids + subclasses, as given in K&B
But in subclasses from different packages, they're accessible only through subclass object, and not through superclass object.

ClassB compiles fine, if following changes are done on Line 8:


This is just the way I understood the concept.
No questions about anything...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, protected members can be accessed only through inheritance in the case, the sub class is in different package! Within the sane package, protected means default access level.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Javaranch I understood the access modifiers fully
Otherwise, I would have easily made silly mistake while answering SCJP6
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic