Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of having public methods when the class is having a default access modifier?

Tags:

java

as for my observation when the class itself is having default access modifier, what is the use of having public methods in it. the java compiler could have stopped using public methods in default class. is there any reason for that?

like image 383
GuruKulki Avatar asked Jan 12 '10 14:01

GuruKulki


People also ask

What is the use of access modifier public in in the main method?

The public keyword is an access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors.

Which is the default access modifier used for the main () method?

By Default the access specifier for Main() in C# is private.

What is the default access modifier of a class?

internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared public , internal , or private . Class members, including nested classes and structs, can be public , protected internal , protected , internal , private protected , or private .

What is the use of access modifiers public in Java language?

Public Access Modifier It is a keyword. If a class member like variable, method, or data members are prefixed with a public access modifier, then they can be accessed from anywhere inside the program. That is, they can be accessed within the same class as well as from outside the different classes.


1 Answers

The non-public class might implement a public interface. This would mean that classes outside of the package could not create an instance of this class or create references of that type, but they would still be able to invoke methods on it if passed an instance.

For example, a public factory class might create an instance of an non-public class in its package and return it.

like image 180
Dan Dyer Avatar answered Oct 17 '22 09:10

Dan Dyer