I have been trying the following code and it is not working. Can anyone let me know what seems to be the problem? (tried on windows 98 and 2000)
my first question is why is the protected amethod() of base class not accessible from derived class and second as to why polymorphism is not working when i use protected method? If i have derived class in the same package as the base class it works fine.
These are the errors being reported. Please point out the error. Thanks. [ May 07, 2002: Message edited by: malini griddaluri ]
Please read JLS 6..6.2 to understand the details of the protected access. b1.amethod is not allowed because the type of the reference (base) is not derived or one of its subclasses. derived d = new derived() d.amethod is ok. The message given by the compiler gives a clue about the problem. [ May 07, 2002: Message edited by: Jose Botella ]
Malini, that's a tricky concept here's what's in my notes hope it helps: 128 A subclass in another package can only access protected members in superclass via references of it's own type(new A().i) or a subtype (new B().i). It is not possible to access it by reference to superclass. In this case of A a = new B() - a becomes reference to the superclass for B
This has been a real surprise for me as I've always heard that listing a method as protected meant that it could be accessed from any class in the same package or a subclass of the enclosing class. I took the above statement to mean that, when considering access to a protected method, I should ask myself the following question: In what type of enclosing class does the line b1.amethod() appear? Is the enclosing class (i.e. derived in the example) in the same package as, or a subclass of, the base class? If so, then the access is permitted. But from all of the above posts, I've obviously been mistaken. I'm wondering if the statement i've written above in bold is just plain wrong, or if I've misunderstood what it means? Could someone help me figure out where I've gone wrong? [ May 07, 2002: Message edited by: Dave Winn ]
Hi All, Thank you very much for the clarification but I have a few more doubts. I perfectly understood what mike kelly was referring to : That you can access it only thru a reference of type derived or any of its sub class and not thru a reference of super class type but when i went to the J.L.S and read that section about the protected method access specification the following sentence seemed a little bit confusing . " A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p). " could any of you explain what this means? what exactly is meant by " it is not involved in the implementation"?
Thanks for this explaination Jose, Corey, Mike et al as I too was somewhat confused about the meaning of protected although I didn't know it until I saw this post! R
Joined: Jul 03, 2001
A derived class is not said to be involved in the implementation of an instance of the base class, because the instance fields of the base class are initialized by the constructor of the base class, not by the constructor of the derived one. We could say that the base class is involved in the implementation of the instances of its subclasses, because once initialized the instance fields, they are inherited by the derived. This concept has buffled everybody reading the JLS, I guess.