File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected methods in different packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected methods in different packages" Watch "Protected methods in different packages" New topic
Author

Protected methods in different packages

malini griddaluri
Greenhorn

Joined: May 07, 2002
Posts: 5
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 ]

Malini
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Its because you are trying to access the base class protected method outside the derived class. If you do this:

in the main everything works fine. If you change the method in the base class to public everything also works fine.
Hope this helps.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
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 ]

Fun programming etcetera!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Take a look at this section of the JLS: §6.6.7 Example: protected Fields, Methods, and Constructors. There is a nice example there that illustrates what is happening in this case.
If you have more questions after looking at that, let me know.
Corey


SCJP Tipline, etc.
malini griddaluri
Greenhorn

Joined: May 07, 2002
Posts: 5
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"?
Rodney Teggins
Greenhorn

Joined: May 06, 2002
Posts: 10
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
How do you call amethod() of base class from derived class?


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected methods in different packages