answersLogoWhite

0

Why toplevel class cannot be private?

Updated: 12/20/2022
User Avatar

Wiki User

12y ago

Best Answer

it can access in entire application.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why toplevel class cannot be private?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a private class?

A private class is a class that cannot be directly accesed from outside its outer class, similar to a private variable or method. This means that a private class must always be an inner class, though an inner class can be public or protected instead. For instance, the following is valid and means that any X object cannot directly access the inner Y class. public class X { private class Y{} } The following however is invalid. private class X{}


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


What does private int mean in java?

The private keyword denotes that the field or method is hidden from view of any other class. class MyClass { private int n; // n cannot be accessed by any class except MyClass private void doSomething(){} // doSomething cannot be accessed by any class except MyClass }


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.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.

Related questions

Why toplevel class cannot be static?

A toplevel class certainly can be static. Static has nothing to do with the level of a class.


What specifies private access modifier and private base class?

A private variable is something that is not visible outside the class. A private class is one that cannot be inherited


What is a private class?

A private class is a class that cannot be directly accesed from outside its outer class, similar to a private variable or method. This means that a private class must always be an inner class, though an inner class can be public or protected instead. For instance, the following is valid and means that any X object cannot directly access the inner Y class. public class X { private class Y{} } The following however is invalid. private class X{}


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


What does private int mean in java?

The private keyword denotes that the field or method is hidden from view of any other class. class MyClass { private int n; // n cannot be accessed by any class except MyClass private void doSomething(){} // doSomething cannot be accessed by any class except MyClass }


Can you define class as private in java?

Yes. However, ONLY nested classes (as in sub-classes of a top-level class) could be declared as private. The main (top-level) class cannot be private as it couldn't be accessed.


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 are the difference between private variable and final variable?

A private variable is one that is accessible only to the current class and cannot be accessed by any other class, including the ones that extend from it. A final variable is one that cannot be modified once it is initialized and assigned a value.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


How do you abbreviate Private First Class?

The abbreviation for Private First Class is PFC.


Can you implement a constructor in private part of code?

no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.


In ANSI C you can hide data by using private access specifier Justify?

One can always declare a datatype as static which will limit the scope to the file only. this way data hiding can be achived. For more clearance on the same please refer 'the C programming language'. Data hiding means only relevant data is visible to the user and all the background information is hidden from the user. In c++, the variables are named as data members and these can be hidden with the help of private access specifier. In procedural languages, variables(global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As, the data members and also member functions of a class cannot be accessed outside the class if they have private access so, they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from the base class then the derived class cannot access the private members of the base class. In addition, if a class is derived from another class privately i.e. for example syntax : class B : private A , is used then all the public and protected members (not private) becomes private to class B and cannot be accessed outside the class B, even by using the object of class B. Hence, data hiding is achieved in C++ through private access specifier.