Answered by: dipl. ing. Dan
Member Since Mar-2006 | Answered On : 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) {
}
}
Answered On : Feb 25th, 2006
hi, no the class can not be protected. the error will be like modifier protected not allowed here.
Answered On : Feb 26th, 2006
The classs can be protected so that it can ve package access only
Answered On : Feb 27th, 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
Answered On : Feb 28th, 2006
Yeah, u can declare class as protected
Answered On : 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.
Answered On : Mar 1st, 2006
We can not declare a class protected the compiler will give an error " protected not allowed ahere"
Answered On : 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
Answered On : Mar 6th, 2006
we can declare the class as protected,only the functionality can be used subclasses.
Answered On : 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 { }}
Answered On : Mar 8th, 2006
HI class can not be declared as protected....
Answered On : 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{}
Answered On : 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.
Answered On : Mar 10th, 2006
Can you also specify how ?? Since declaring a class as protected DOES give compilation error
Answered On : 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
Answered On : 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.
Answered On : Mar 20th, 2006
Yes we can declare a class as protected but it should be an inner class
Answered On : Mar 21st, 2006
No class canot declared as protected or private only public and default access modifier is allowed.
Answered On : 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.
Answered On : 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
Answered On : 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 allowedpublic class ProtectedOrNot { // Inner class - public, default, protected, private allowed protected class InnerClass { } public static void main(String[] args) { }}
Answered On : 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
Answered On : 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.
Answered On : 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 publicto declare class."modifier protected not allowed here"
Answered On : 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.
Answered On : Apr 3rd, 2006
We can declare the protected to class..Regards,Shan
Answered On : 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.
Answered On : Apr 7th, 2006
if class declared protected all var,methods can not be access by the other packageor same package
Answered On : 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(); }}
Answered On : May 11th, 2006
We can declare class as protected and class will be available to all subclasses and other classes in same package.
Answered On : 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.
Answered On : 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 answerThank u
Answered On : Jan 4th, 2008
No u can not declare class as protected......
Answered On : 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.
Answered On : Mar 27th, 2008
No, We cannot hahve a "protected" class. The only possible values are "public", "abstract" & "final"(apart from "default").
Answered On : May 19th, 2008
No we cant declare class as protected
Answered On : 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
protected class xyz{ }
Answered On : Jan 20th, 2012
The class cannot be declared as protected..
Answered On : Mar 19th, 2013
Class can be protected or private but only inner
Answered On : 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.
Answered On : 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
Answered On : 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
public class A //only public,abstract,final allowed for primary class { //B is inner class protected class B //allowed in java. You can use any access modifier here { } }
Copyright © 2022 GeekInterview.com | All Rights Reserved
Copyright © 2022 GeekInterview.com | All Rights Reserved