• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with access modifier protected  RSS feed

 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • 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?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Okay thank you.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you guys, slowly making sense
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome We try our hardest and people always say I am trying.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!