AllExperts > Experts 
Search      

Java

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Neal 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.
 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > Core java(visibility of classes)

Topic: Java



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.  

You can read about inner classes (nested classes) in any good
Java book, or here:
http://www.retrologic.com/innerclasses.doc.html


Other than its use with inner classes, the "protected" keyword
does not apply to classes.

...nz


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.