• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a protected question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Because B is not a subclass of C, ..."
[ December 23, 2003: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 58
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic