AboutNeal Ziring Expertise Almost anything about the core Java platform, with an emphasis on networking. Not familiar with JDBC or the new Java graphics APIs. [Sun Certified Java Programmer, JDK 1.1]
Experience Certified Java Programmer for JDK 1.1 and 2.0.
Expert: Neal Ziring Date: 6/24/2008 Subject: Core java(visibility of classes)
Question Protected visibility- A variable or method declared protected can be used only by classes in the same package or in a derived class in the same or different package.
my question is..Is protected keyword used with classes? or is it only for variable or method..
sorry if my understanding and my question is wrong..i am new to java and i want to learn the language..
Thankyou
Answer Ms. Ranjan,
The keywords "protected" and "private" can be used with classes,
but ONLY with certain inner classes (nested classes).
For example, the code below is wrong:
package foo;
protected class TopLevel {
. . .
}
but the nested class code below is legal:
package foo;
public class TopLevel {
private int x;
protected class Nested {
. .
}
. . .
}
If you are new to Java, then you probably haven't used nested
classes. The nested class shown above is properly called a
'member class' because it is part of the enclosing class.