• 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

Confused about a question in mock exam regarding access modifiers

 
Greenhorn
Posts: 15
MS IE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I state the following...

public - everyone can access methods and attributes of the class
protected - class, and every subclass of class no matter of the package can access
private - only class can access
default - class, and every subclass can access if they are in the same package

accesscontrol

Question im having trouble with:


another package... another class...
[Added code tags - see UseCodeTags]

This gives a compilation error, any suggestions as to what mistake i might have made?

/Danjel
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected can be a bit tricky. protected members are accessible from another package via inheritance. That means that, in your example, a GoldAccount object can access it's own accountNo variable, but you can't access the variable of another object. That's what you're trying to do in that code.
 
Danjel Nyberg
Greenhorn
Posts: 15
MS IE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Realized that only minutes after posting.. sometimes it is good to just write it down... got it now.

Thanks
/Danjel
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also your definition of default is incorrect.

Default - can be accessed by classes which are in the same package as the class whose member you are trying to access. if a class is declared to be default scope(also called package private) it can only be accesses by other classes in the same package.

Please feel free to post if you have doubt

Regards
 
Danjel Nyberg
Greenhorn
Posts: 15
MS IE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Not quite sure how that differs from what i stated above. A Default accessed attribute in a superclasss is accessible in a subclass of superclass as long as they are in the same package.

In difference here from protected:

The above method would work if i change: Account acc = new Account(); to GoldAccount acc = new GoldAccount();
as they are in different packages, but if accountNo would have been declared with default access, this should not work. (if they are in different packages)

/Danjel
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Danjel Nyberg wrote:Not quite sure how that differs from what i stated above. A Default accessed attribute in a superclasss is accessible in a subclass of superclass as long as they are in the same package.


That's accurate, but not complete (which you may have realised, but it's not clear). Default accessible members are accessible to any class in the same package, not just subclasses.
 
Danjel Nyberg
Greenhorn
Posts: 15
MS IE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course... absolutely correct, and so are protected (in the same package)

/Danjel
reply
    Bookmark Topic Watch Topic
  • New Topic