aspose file tools*
The moose likes Beginning Java and the fly likes Protected method in a packaged superclass not being called by subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected method in a packaged superclass not being called by subclass" Watch "Protected method in a packaged superclass not being called by subclass" New topic
Author

Protected method in a packaged superclass not being called by subclass

kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Hi,
This is my first post to this forum. I have started preparing for SCJP recently and I am totaly confused with the following code.
Can't find an answer by myself.

Fruit.java is in a package foo.

package foo;
public class Fruit
{
protected void k()
{
}
}



And implementing class Apple is having default access.

import foo.Fruit;
class Apple extends Fruit
{
public static void main(String a[])
{
Fruit b = new Apple();///Code fails to compile...If the new Apple object is assigned to a Apple b object then it runs fine...
b.k();
}
}
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
What's the error message?
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
John McClellan wrote:What's the error message?


k() has protected access in foo.Fruit...

Something very basic I am missing here....
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
What do you know about access levels? Do you know what these mean(?):
public
protected
default
private
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
I know the basic meaning...May be not the in depth one...
If you see what I am doing wrong in code...please point it out...
I am still unable to figure it out...
James E Baker
Greenhorn

Joined: Jul 28, 2011
Posts: 23
Welcome to JavaRanch!

First, if Fruit and Apple are in the same package, you don't need to import Fruit.

Second, I copied and pasted your posted code and it ran just fine. See below:

Output : Fruit foo!
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Hi...You are right..If both are in the same package we don't need to import...

But if you see my code, Apple is not in same package.

Fruit is in foo package and Apple is outside the foo directory having default access...

Your code runs fine...I tried it...

I again ran my code just to be sure..It doesn't compile...

Thanks for replying...
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
I think I am getting somewhere with the following explanation,

At compile time, b is treated as a Fruit object...Now calling k() on Fruit object is not possible because protected makes accessible only through inheritance.

That's why when we assign it to Apple type it works fine...

Let me know if I am correct here...
James E Baker
Greenhorn

Joined: Jul 28, 2011
Posts: 23
Yeah...

Sorry, I didn't catch that the two classes were in different packages. That makes more sense.
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
That's fine..I too didn't use code tags...
Anyway so what do you say about the explanation I gave?
Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29

Now I am only a beginner myself, but what you said there about inheritance sounds about right. Here is perhaps a better answer from Head First Java 2nd Edition:

One of the interesting things about protected is that-unlike the other access levels-protected access applies only to inheritance. If a subclass-outside-the-package has a nference to an instance of the superclass (the superclass that has, say, a protected method), the subclass can 't access the protected method using that superclass referencel The only way the subclass can access that method is by inheriting it. In other words, the subclass-outside-the-package doesn't have access to the protected method. it just has the method, through inheritance


Hope it helps
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Daniel Vlad wrote:
Now I am only a beginner myself, but what you said there about inheritance sounds about right. Here is perhaps a better answer from Head First Java 2nd Edition:

One of the interesting things about protected is that-unlike the other access levels-protected access applies only to inheritance. If a subclass-outside-the-package has a nference to an instance of the superclass (the superclass that has, say, a protected method), the subclass can 't access the protected method using that superclass referencel The only way the subclass can access that method is by inheriting it. In other words, the subclass-outside-the-package doesn't have access to the protected method. it just has the method, through inheritance


Hope it helps


It totally helps...Thanks a lot...
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

kunal Agarwal wrote:That's fine..I too didn't use code tags...
Anyway so what do you say about the explanation I gave?


If both classes are in in the same package they work fine, protected do not work from out side world. in other words from other package. But yo can used inheriting class to use it {k() with Apple reference }.

please go through this link

http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.7
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Excellent link Rahul.

The new info added to our previous discussion -
The method delta could try to cast (§5.5, §15.16) its parameter to be a Point3d, but this cast would fail, causing an exception, if the class of p at run time were not Point3d.


So, in this case the code will comply. But at the run time if the object doesn't turn out to be the right object it will throw an exception, otherwise everything will be fine.

Thanks a lot guys...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected method in a packaged superclass not being called by subclass