my dog learned polymorphism*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a protected question" Watch "a protected question" New topic
Author

a protected question

cheng hsien lee
Greenhorn

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();

c.sh();//line 1 compiler error:sh() has protected access in test.A
b.sh();//line 2
}
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
Why does the line 1 compiler error but line 2 have no problem???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10

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

JavaBeginnersFaq
"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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 C.sh(), but if there were a class D that extended B, B could call D.sh(). Surprising though it may seem, B is not allowed to call C.sh() (or A.sh()), but only B.sh(); 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
    c.sh();
    b.sh();
    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!!
    Arios
    A learner
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    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 http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36201 .
    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.
     
    wood burning stoves
     
    subject: a protected question