Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a class private or public by default in Java and C++?

Are classes private or public by default in Java and C++?

like image 821
the_learner Avatar asked Sep 09 '12 05:09

the_learner


People also ask

Is class private in Java?

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier). If it does not have a modifier, it is supposed to have a default access.

Is Java class always public?

No it doesn't. Unless it's public, the class won't be visible to other code which isn't in the same package. The default accessibility (which can't be specified explicitly) is that a class (or other member) is only visible to other code within the same package.

Are classes default private?

By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.

Is public or private in Java?

public means you can access it anywhere while private means you can only access it inside its own class. Just to note all private, protected, or public modifiers are not applicable to local variables in Java. a local variable can only be final in java.


1 Answers

  • Java:

    By default, the classes visibility is package private, i.e. only visible for classes in the same package.

  • C++:

    The class has no visibility defined like in Java. They are visible if you included them to the compilation unit.

like image 80
zeller Avatar answered Sep 21 '22 01:09

zeller