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 a protected question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a protected question" Watch "a protected question" New topic

a protected question

cheng hsien lee

Joined: Nov 23, 2003
Posts: 1
package test;
public class A{
protected static void shoot(){System.out.print("shoot");}
protected void sh(){System.out.print("sh");}

package test1;
import test.A;
class C extends A{}
public class B extends C {
public static void main(String[] argv){
C c=new C();
B b=new B();;//line 1 compiler error:sh() has protected access in test.A;//line 2
Why does the line 1 compiler error but line 2 have no problem???
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Outside of the package where they're defined, protected methods can be called only by code in the class where the method is defined or in a subclass of that class. Because B is not a subclass of C, line 1 is in error. It's a subtle point.

[Jess in Action][AskingGoodQuestions]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9044

"Because B is not a subclass of C, ..."
[ December 23, 2003: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Azam Bukhari
Ranch Hand

Joined: Oct 03, 2000
Posts: 58
hi folks
okay what do U guyz meant to say that B is not the subclass of C
when it is typed there that
public class B extends C{
I am confused help me out...sherrif!!

Don't wanna invent the wheel again!!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Sorry, my explanation is terrible -- and wrong, since I didn't look closely enough at your code. Let me try again.
Outside of the package where a protected member is defined, you can call protected members only
  • Inside a class that inherits the method
  • On a reference that is of that class's type, or one of its subtypes.

  • B can't call, but if there were a class D that extended B, B could call Surprising though it may seem, B is not allowed to call (or, but only; that's just the rule.
    Compilers disagree about how these rules apply to static protected methods (anticipating your next question) because static methods aren't polymorphic.
    Some compilers will let B call A.shoot(); others will only allow you to write B.shoot(), even though both refer to the same method.
    Azam Bukhari
    Ranch Hand

    Joined: Oct 03, 2000
    Posts: 58
    well this one protecte question is takng me on the knuckles...
    I really appreciate what sherrif here has tried to teach...but I wonder why ur concept is conflicting with what we usually learned...
    Sherrif I only want to share & learn from ur experience, so if I am pouring something black out here pls. obliedge me!!
    Folks we have a straight hierarchy of three classes with top one lets say as Grand Father...middle one as father ..and the last one as child.
    Grand father is living in one house and the father and the child bth are living in one.
    1. Now if something that is being inherited by father is also inherited by the child by default....
    2. As per the last response the sherrif poured in, I believe there is no polymorphism going on in here,
    the object of class C(Father) is trying to call its inherited sh() and object of class B(Child) is trying to call its own, unfortunately both have no definition neither declaration, of sh() so at both the events of;;
    the sh() will propgte upwards through the hieraarchy finding the method sh()...which both objects will find in he grand pa class.
    I had tried to run the code here, but i got the same error, either books are wrong, or what the junk is goin around in here!!
    Clear It Up Sherrif...In simple words..I will be more than thankful!!
    A learner
    Ernest Friedman-Hill
    author and iconoclast

    Joined: Jul 08, 2003
    Posts: 24183

    Azam --
    My explanation above is correct. The rule is that a class C1 can't access a protected method declared in class C2 through a reference to some subclass of C2 named C3, unless all the classes involved are in the same package, or unless C3 is a subclass of C2. The fact that C2 is a subclass of C1 and can access the method that way is irrelevant.
    This is described in section 6.6.7 of the Java Language Specification; see .
    Marilyn was not trying to explain anything; she was just pointing out my error: while I stated that B is not a subclass of C, your code in fact showed that it was.
    I agree. Here's the link:
    subject: a protected question
    Similar Threads
    Dans exam
    Why compilation fails (return type reference type , object return
    Overloading and Overriding
    Dans mock exam -Question on protected access
    Inheritance, Access and Packages