File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Related to protected method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Related to protected method" Watch "Related to protected method" New topic

Related to protected method

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Friends,
I have a very basic doubt related to protected access specifier...Kindly see the following code ...I am not getting why there is a compile time error..I am using JDK1.4....
Class Abcd in Package x

Class Pqrs in package y extends Abcd in x


C:\>javac -d . display() has protected access in x.Abcd

Thanks in Advance
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
6.6.2 Details on protected Access [B]
A protected member or constructor of an object may be accessed from outside the package in which it is declared [b]only by code that is responsible for the implementation of that object.

In the code instance of class Abcd is trying to call the protected method which is implemented by class Pqrs. So the error.

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Harshil,
Thanks for Quick Reply...Now even if i comment the code of display()in Pqrs(Referring to code given earlier)i amgetting the same error
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
I disagree with that interpretation. Here's my understanding:

When a class has a protected member, it is available to subclasses outside the package through inheritance only. That is, a subclass can't access a protected member of an instance of the superclass, but it can access an inherited copy of that member in an instance of the subclass. So, Pqrs can't access Abcd.display().... but it can access Pqrs.display(), which is inherited from Abcd. Here's the example that compiles and runs for me:

This class is unchanged:

When the y.Pqrs class is run, the results are: "In Abcd"

(If you change the name of the "Pqrs" method back to "display()", you get "In Pqrs" - but it becomes an uninteresting case, since you would have the same result if the Abcd class never existed, or the Abcd.display() method was private...)

Hope this helps.

-- Jon
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Jon,
Thanks a Lot for quick reply....
well there is one more change i have observed in your reply

Do you want to say that if i create the instance of Abcd class in Pqrs's main then also i can't access protected method & variable of Abcd
something done in original code.....

Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
Yes shrinivas John meant the same. U cant access the display() function by creating an instance of Abcd class in the Pqrs class main.

SCJP 5.0 77%
I agree. Here's the link:
subject: Related to protected method
It's not a secret anymore!