• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic