• 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

Trouble with access modifier protected

 
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm learning about access modifiers and Protected is doing my head in. I thought I understood it but I can't get the following code to work. Before I go crazy can someone tell me what I'm doing wrong here please.

From my understanding a protected modifier is similar to default with the addition that it can also be accessed in another package via the use of a subclass......Is this correct?


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fergus Flanagan wrote:From my understanding a protected modifier is similar to default with the addition that it can also be accessed in another package via the use of a subclass......Is this correct?



Yes.

Fergus Flanagan wrote:



You forgot about the "responsible for the implementation" requirement.

http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

In this case, since the accessing code is from the AnotherClassInAnotherPkg class, the instance type must be IS-A AnotherClassInAnotherPkg instance.

Henry
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay see my mistake not sure I fully understand it, it sort of makes sense. The point of extending the class is for increased functionality in the new objects which inherit from the parent class. I was using the parent objects when I should have been using the child objects.

the instance type must be IS-A AnotherClassInAnotherPkg instance.

What does "IS-A" mean?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fergus Flanagan wrote:What does "IS-A" mean?


Say you have a class Animal and you want to create a subclass Dog. Does this pass the IS-A test? "A dog IS-A animal". Yes, so the subclass makes sense.
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thank you.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You will see things like
public class Car extends Vehicle
because a Car ”IS‑A“ Vehicle. It is one of the rules of inheritance.
You should never see things like
public class Car extends Wheel
because a Car ”IS‑NOT‑A“ Wheel.
When you learn about composition and aggregation you will find that
a Car ”HAS‑A“ Wheel. (Well, usually several wheels.)

The code which can access protected members must be able to say it is an instance of a subclass, whether in the same package or in a different package. It does cause quite a bit of confusion to lots of people.
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys, slowly making sense
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome We try our hardest and people always say I am trying.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic