Can we declare class as protected?

What is the scope for this class?

Editorial / Best Answer

dipl. ing. Dan  

  • Member Since Mar-2006 | Mar 24th, 2006


Some people might get confused by the many posts. The correct answer to the question "can we declare class as protected?" is YES, BUT only as an inner class. A 'normal' class cannot be protected or private.

// 'Normal' class - only default or public allowed

public class ProtectedOrNot {

  // Inner class - public, default, protected, private allowed

  protected class InnerClass {

  }

  public static void main(String[] args) {

  }

}

Showing Answers 1 - 65 of 65 Answers

vimal

  • Feb 25th, 2006
 

hi,

        no the class can not be protected. the error will be like modifier protected not allowed here.

  Was this answer useful?  Yes

murugan

  • Feb 26th, 2006
 

No No we should not declare the class as protected or private the compiler will show the error only possible modifiers are public and default

  Was this answer useful?  Yes

navneet raushan

  • Feb 28th, 2006
 

Yeah, u can declare class as protected

  Was this answer useful?  Yes

Sachin Soni

  • Mar 1st, 2006
 

Yes, we can declare a class as protected, however, its sub-classes (inherited) can only use the objects of that class then. It will not be possible to access the functions of that class with other class's objects.

  Was this answer useful?  Yes

minnie

  • Mar 1st, 2006
 

We can not declare a class protected the compiler will give an error " protected not allowed ahere"

  Was this answer useful?  Yes

kotesh

  • Mar 5th, 2006
 

1. No , You can't declare a class as protected. because compiler will give you Compile time error.2. You can decalre a class a public or default. if you don't mention any access specifier by default it will take class as default access specifier

  Was this answer useful?  Yes

narendra

  • Mar 6th, 2006
 

   we can declare the class as protected,only the functionality can be used subclasses.

  Was this answer useful?  Yes

ananthi

  • Mar 7th, 2006
 

Yes.. A class can be declared as protected.But it should not be the outermost class.Example:public class A{ protected class B { }}

  Was this answer useful?  Yes

Shyam sankar

  • Mar 7th, 2006
 

HI

  class can not be declared as protected....

  Was this answer useful?  Yes

Ranjith

  • Mar 9th, 2006
 

Hi All,

We can not declare class at very beginning like:

protected class ProtectedCheck{

.................................

}

However, One can declare innerclass as protected :

class A

{

}

  Was this answer useful?  Yes

Ranjith

  • Mar 9th, 2006
 

Hi All,

We can not declare class as protected at very beginning like:

protected class Sample {

........................

}

However, One can declare class as protected inside other class(inner classes).

We all know how to use inner classes, if not pls refer to java.sun.com or the same site for details.

  Was this answer useful?  Yes

sunflower

  • Mar 10th, 2006
 

Can you also specify how ?? Since declaring a class as protected DOES give compilation error

  Was this answer useful?  Yes

parthiba

  • Mar 10th, 2006
 

D:\Tomcat5.0\webapps\servletproject\src>javac -d ../WEB-INF com/college/*.javacom/college/Hello.java:2: modifier protected not allowed hereprotected class Hello ^1 errorhi friends look up this error if we declare protected the error will arise so modifier protected is not allowed here in the classbye

  Was this answer useful?  Yes

s_s78

  • Mar 20th, 2006
 

Hi all,

make it clear 1. you can declare a class as protectd and private only if it is member of any other class and since this class is member of a class it may have all class-member access modifiers.

2. a simple class can have only public or default scope.

  Was this answer useful?  Yes

gopalakrishna

  • Mar 20th, 2006
 

yes we can declare a class as protected but it should be an inner class

  Was this answer useful?  Yes

vaibhav

  • Mar 21st, 2006
 

No class canot declared as protected or private only public and default access modifier is allowed.

  Was this answer useful?  Yes

Ranjith

  • Mar 21st, 2006
 

All these discussions can be stopped now :-). A class can be declared as protected , if it is an inner class. Plase check for inner classes in java.sun.com.

  Was this answer useful?  Yes

JavaStudent

  • Mar 22nd, 2006
 

Hello friends, good to see so many comments, but it will be even better if people confirm with their answers and then put it in the forum and not just make guesses, bcos many of our friends use this for interview purposes. so please enter the right answers and dont reply just for the sake of replying.

Good Luck and Cheers

  Was this answer useful?  Yes

Some people might get confused by the many posts. The correct answer to the question "can we declare class as protected?" is YES, BUT only as an inner class. A 'normal' class cannot be protected or private.

// 'Normal' class - only default or public allowed

public class ProtectedOrNot {

  // Inner class - public, default, protected, private allowed

  protected class InnerClass {

  }

  public static void main(String[] args) {

  }

}

kami

  • Mar 25th, 2006
 

Hi,

     Yes, We can declare a  class as protected.The accessibility of the class is sub classes in the same package and sub classes of  other packages also

 

  Was this answer useful?  Yes

Rahim

  • Mar 27th, 2006
 

No, you can't give a modifier protected to a class. It gives the compille time error "modifier protected not allowed here".

Regards,

ashok.

  Was this answer useful?  Yes

Mittal

  • Mar 28th, 2006
 

No. u cant declare class as protected it will give u compiler error.

u cant declare as private also, there is only two modifier 1 default, 2 public

to declare class.

"modifier protected not allowed here"

  Was this answer useful?  Yes

Harihara dash

  • Mar 28th, 2006
 

Yes, a class can be declared as protected but aTOP level class can not be declared as protected,any other classes can be declared as protected.A top level class can only declared public ,final,abstract and default.

  Was this answer useful?  Yes

Shanmugasundaram Sellappan

  • Apr 3rd, 2006
 

We can declare the protected to class..

Regards,

Shan

  Was this answer useful?  Yes

vikas sahu

  • Apr 6th, 2006
 

There are two posssible way, First you can not use protected with the outer class, second you can use protected if your class is inner class.

  Was this answer useful?  Yes

mahab

  • Apr 7th, 2006
 

if class declared protected all var,methods can not be access by the other packageor same package

  Was this answer useful?  Yes

Renukaprasad

  • May 5th, 2006
 

Dear all, We can declare a class with any modifier (private, public, procted & default/package level) only for inner classes same is not applicable for outer class. Outer class can be public / abstract / final . No other specifiers are allowed.Eg:package classAndObjects;/** * @author Renukaprasad C * @May 5, 2006 * *///protected class ClassMidifier //Not OK gives Compiler errorpublic class ClassMidifier{ private class InnerClass { public void innerMethed() { System.out.println("Inside innerMethod() "); } } public static void main(String[] args) { ClassMidifier classMidifier = new ClassMidifier(); InnerClass innerClass = new ClassMidifier().new InnerClass(); innerClass.innerMethed(); }}

  Was this answer useful?  Yes

abdul

  • May 11th, 2006
 

We can declare class as protected and class will be available to all subclasses and other classes in same package.

  Was this answer useful?  Yes

Manish Kumar Singh

  • Jul 16th, 2007
 

No we can't declare class protected because we know that when we declared class
Protectecd then it's mean that we can access class inside our package only.

  Was this answer useful?  Yes

riyaz

  • Nov 16th, 2007
 

NO, we cant use the class as protected and private. The class can be used as public or default access specifier. if we use private or protected to the class it will give error definetly. if wrong, pls send corect answer


Thank u

  Was this answer useful?  Yes

mabhijit

  • Jan 9th, 2008
 

As we talk about the top level of any class
e.g:- class MyClass {
}
then here only "public" or bydefault "default" (It means if no access modifier is defiened then compiler by defult use "defualt" accessifer) access modifires is used.
But if we are talking about the inner classes(Nested classes)
e.g. :- class MyClass {
               class MyNestedClass {
               }
          }
so in this inner class (MyNestedClass)  we can use any of the access modifiers.

Thanks.

  Was this answer useful?  Yes

sumanmshan

  • Mar 27th, 2008
 

No, We cannot hahve a "protected" class. The only possible values are "public", "abstract" & "final"(apart from "default").

  Was this answer useful?  Yes

manish

  • Oct 17th, 2011
 

We can not declare a class as protected or private
compiler will gives an error like ;
xyz.java:1: modifier protected not allowed here
protected class xyz{
^
1 error

Code
  1. protected class xyz{

  2. }

  3.  

  Was this answer useful?  Yes

prakash

  • Mar 18th, 2013
 

Class can be protected or private but only inner

  Was this answer useful?  Yes

Raghunath

  • Aug 16th, 2013
 

A class cannot be declared as a protected as its scope will not allowed for different classes within the package or outside the package. public / abstract / default / final can be declared but not protected.

  Was this answer useful?  Yes

Srinu

  • Jul 8th, 2017
 

We cannot declare class as protected and private it gives compile time error.
Illegal modifier for the class TestClass; only public, abstract & final are permitted

  Was this answer useful?  Yes

vishal

  • Sep 21st, 2017
 

We cannot declare primary classes as protected for primary classes only "public","final","abstract" are allowed.
But point to be noted you can declare inner classes as protected.

Code
  1. public class A //only public,abstract,final allowed for primary class

  2. {

  3.   //B is inner class

  4.   protected class B //allowed in java. You can use any access modifier here

  5.    {

  6.    }

  7. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions