aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected" Watch "Protected" New topic
Author

Protected

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
Hi!
The answer given for the following code is that the error occurs at lines 3 and 4. I feel that line 2 should be included since protected methods are accessible only thro inheritance and not by creating an instance of class.Am I rite?can somebody pl help?Thanks


(CODE TAGS ADDED TO MAKE IT MORE READABLE)
[ May 15, 2004: Message edited by: Barry Gaunt ]

Software_guy
Sandeep Advani
Ranch Hand

Joined: Mar 11, 2004
Posts: 78
Accessibility to protected members of superclass is permitted via references of subclasses (and not reference of its superclass). In a nutshell, protected members of a class are accessible by any class in the same package and by those subclasses of its class in other packages. Hope this helps.

Sandeep
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What happens when you compile the two classes?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sandeep Advani
Ranch Hand

Joined: Mar 11, 2004
Posts: 78
Hello:

m3() will have private access in A

m4() is not public in A and cannot be accessed from outside the package.

Is this what you are looking for ?

Sandeep
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sorry Sandeep, I'm asking Sridhar the question.
Sandeep Advani
Ranch Hand

Joined: Mar 11, 2004
Posts: 78
My bad. The error I explained above was from Sridhar's code. I should have mentioned that.

Now what I said before is explained better in following code.

package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
C c1 = new C();
public static void main(String[] args) {
C c = new C();
c.m1(); // 1
c.m2(); // 2
//c.m3(); // 3
//c.m4(); // 4
}

void m5(A objRefA) {
c1.m2();
objRefA.m2(); //m2() has protected access and cannot be referenced from its superclass. Only by its subclasses. (Different packages are assumed.)
}
}

I am only trying to focus on m2() which is protected.

Thanks
Sandeep
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
Thanks Sandeep. I got it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected