answersLogoWhite

0


Best Answer

Yes, but not in a normal way.

Sample code:

namespace WikiAnswers {

public class MainClass {

protected class HereItIs {}

}

}

The inner class MainClass.HereItIs is the answer to this question.

If you change the accessibility of MainClass from public to protected, the compilation will fail.

My interpretation of the keyword protected is: "accessible to the class itself and its derived classes", and may only decorate/mark it to class elements (data members, methods, properties, etc.)

Noramlly, we declare a class as a namespace element, e.g. MainClass above, I almost answered 'NO' to this question when I read the question the first time. Tricky.

The above example may be applicable to Java:

1. replace namespace to package

2. the rest of the codes should be the same

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

no, java does not support a class declared as private. Because of java's high security java virtual machine [jvm] does not allow any data to be declared as private. infact, java has its own codes that are private to itself and thus cannot be accessed by any of us.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it possible to declare class as private?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you make private class in c plus plus?

yes it is possible to make a private class in C++ but this class will not solve any purpose.................


Can you declared constructor as private?

If you declare the main method anything other than public, it will not run. If you declare main method as private, you would not be able to execute the class as a standalone java program. Any java class that needs to be executed as a standalone file needs to have a main method that is public, static and returns a void. Otherwise the Java compiler would not recognize the file as an executable standalone java file and would not allow you to run it.


Why do you member functions defined outside the class?

A member function of a class can be defined outside the class using scope resolution :: operator Thanks Rajneesh


How do you access private variable in flex?

In Flex, you access a private variable by calling it from within that class. Remember, the "private" modifier means it cannot be directly accessed outside of the class you declare it in (note I say "directly accessed" you can indirectly access it via public functions which I will show below). Example: declare your variable at the top of your class like this: private var myVariable:String; Then, inside one of your functions of that class, you can access the variable and/or assign it this way: public function changeMyVariable(value:String):void { // sets the private variable to a custom string myVariable="test String"; trace("myVariable is set to "+myVariable); // sets the private variable to the argument passed in myVariable = value; trace("myVariable is now set to" +myVariable); }


What are the privileges granted to friend function?

Friend functions (and classes) have private access to the classes that declare them as friends. Although they have the same access rights as members of the class itself, friends are not themselves members of the class and cannot be inherited.

Related questions

Can you make private class in c plus plus?

yes it is possible to make a private class in C++ but this class will not solve any purpose.................


Can you declared constructor as private?

If you declare the main method anything other than public, it will not run. If you declare main method as private, you would not be able to execute the class as a standalone java program. Any java class that needs to be executed as a standalone file needs to have a main method that is public, static and returns a void. Otherwise the Java compiler would not recognize the file as an executable standalone java file and would not allow you to run it.


What do you mean by classes within the classes?

You can declare and implement a class within a class...class abc {...class def {...}}The class def is scoped and known only within the context of abc. It is possible to declare instances of def as member variables of abc, however, for this to work correctly, they should be private, because methods of def, not even public methods, do not exist outside of the scope of abc.


Can you declare default constructor as private?

Yes, but that means you can't define an instance of the class without arguments to new.


What is friend datatype in object-oriented programming?

A friend is any class, class method or function that is declared to be a friend of a class. Friends have private access to the classes that declare them friends.


What if you declare public members rather than private in c plus plus?

Public members in C++ have accessibility to any function that has scope to the instance of the class, whereas private members have accessibility only to functions of that class.


Why do you member functions defined outside the class?

A member function of a class can be defined outside the class using scope resolution :: operator Thanks Rajneesh


How can you create a class which cannot be inheritable?

Declare it final.


Declare a class called rectangle which has two private members length and width and two public member functions called input and output?

class Rectangle{ private: int length; int width; public: void input(); void output(); };


Is it possible to access the private variable in CPP from outside the class?

Private variables can only be accessed from outside of a class by using any public function of that same class. Or this can be accomplished by using Friend functions.


How do you access private variable in flex?

In Flex, you access a private variable by calling it from within that class. Remember, the "private" modifier means it cannot be directly accessed outside of the class you declare it in (note I say "directly accessed" you can indirectly access it via public functions which I will show below). Example: declare your variable at the top of your class like this: private var myVariable:String; Then, inside one of your functions of that class, you can access the variable and/or assign it this way: public function changeMyVariable(value:String):void { // sets the private variable to a custom string myVariable="test String"; trace("myVariable is set to "+myVariable); // sets the private variable to the argument passed in myVariable = value; trace("myVariable is now set to" +myVariable); }


Sample code of typedefstruct in c plus plus?

You don't need a typedef to declare a struct in C++. A struct is declared exactly the same way that you would declare a class, the only difference being that struct members are public by default, while class members are private by default. Aside from that the class and struct keywords are completely interchangeable.