• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphically access a protected method  RSS feed

 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it that a protected method cannot be accessed polymorphically outside its package. I suspect the reason is that some compile-time checks have to be performed. Is this correct?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's nothing to do with compile-time checks. It's simpler than that -- you can't do that because the rules of Java (the Java Language Specification) say you can't do that.

Now, you may already have known that was the rule and meant to ask what was the purpose of that rule versus some other rule. Was that it?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I'd like to know the purpose of that rule. The rules of Java say you can't do that but there must be a reason for disabling polymorphism.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but bear in mind that I wasn't one of the people who thought up the rule!

The idea is that a package is supposed to be a group of closely-related classes. That being the case, it makes sense that they should be able to share data between them without that data having to be public. Making it public causes it to be accessible to everybody in the whole world, which might not be a good idea.

Now what I'm describing here is actually what's referred to as "package-private" or "default" access, which is achieved by not putting any modifier on the method. Protected access works the same as that, except that protected members of a class are accessible not only to other classes in the same package, but also to subclasses of that class. (Which might be in different packages.)
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd venture that avoiding name collisions is also a benefit of package-private access. That is, withing a package, you're free to use any name you like for a class, and that also means you are not foreclosing users of your package from using the same names in their own packages. (For those who manage networks, it's a bit like the fact that all 192.168.*.* subnets are independent of each other. In my network, 192.168.1.2 is my computer; in your network, 192.168.1.2 can be your computer; so long as we communicate through some other entity with each other, there's no collision. In my package, I can have a class called "Stuff." In your package, you can also have a class called "Stuff." As long as the access to each of our packages is through some other public class, there's no name collision.)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:Why is it that a protected method cannot be accessed polymorphically outside its package. I suspect the reason is that some compile-time checks have to be performed. Is this correct?


I seem to be missing the point of this topic. By "accessed polymorphically", I am assuming that the protected method is access by the subclass? Of course a protected method can be accessed by its subclass, even if the subclass is not in the same package.

Since everyone seems to understand the question, can anyone elaborate? I seem to be slow this morning...

Henry
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code illustrates the question. From the answers kindly provided, I now understand that when a protected method is called outside the package where it is defined, the compiler simply does not allow the protected method to be called using a reference that is not declared as a subclass i.e. the compiler does not allow the protected method to be called polymorphically.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:The following code illustrates the question. From the answers kindly provided, I now understand that when a protected method is called outside the package where it is defined, the compiler simply does not allow the protected method to be called using a reference that is not declared as a subclass i.e. the compiler does not allow the protected method to be called polymorphically.


What you ran into is a not-well-known part of the Java Language Specification ...

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

It is used to protect subclasses from each other. Otherwise it would be possible for one subclass to access the superclass components of another subclass.

And yes, since this check is done at compile time, it uses the reference to confirm the type.

Henry
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:It is used to protect subclasses from each other. Otherwise it would be possible for one subclass to access the superclass components of another subclass.

Thanks. This reason makes a lot of sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!