Java Public Keyword
A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A member's access is declared to be public with the Java keyword public. All other classes can see members of the public class. This implies that a public field or method can be accessed by any other class. In addition, public fields can be changed by other classes unless they are marked as final.
It is recommended to make fields private and to only grant public access to the methods and final fields that establish the public constants for the class. This aids with encapsulation and information hiding since it enables you to update a class' implementation without having an impact on users who only access the class' public API.
Any class may access a stated public class, method, function Object () {[native code]}, interface, etc. As a result, any Java Universe class can access properties, methods, and blocks declared inside a public class. A public class named Length that preserves private instance fields with the names units and is an example of an immutable class.
Important Points to Remember about public keyword:
- Everywhere has access to the public access modifier. As a result, we have easy access to both the class and package's public.
- Any method that is being overridden must not be more restrictive than the original method, which must be stated in the subclass. Therefore, if you give a method or variable the public access modifier, only the public access modifier can be used to override it in a subclass.
- A program may only designate one class as public if it has many classes.
- The name of the program must be like the name of the public class if a class contains one.
Examples of Public Keyword
Here is an illustration of how to tell whether a public variable and method is used outside of the class. We also attempt to make a function Object () {[native code]} instance outside of the class.
class HH {
public String msg="outside the class the public variable is accessed";
String info;
public void disp()
{
System.out.println("outside the class the public variable is accessed");
System.out.println(info);
}
Public HH (String info)
{
this.info=info;
}
}
public class PublicEx1 {
public static void main (String [] s) {
HHh=new HH ("outside the class trying to create the instance of public constructor ");
System.out.println(h.msg);
h. disp();
}
}
Output:
outside the class the public variable is accessed
outside the class the public variable is accessed
outside the class trying to create the instance of public constructor
Example 2:
An illustration of how to check whether a public variable or method is usable outside of the package. Here, we also attempt to make a function Object () {[native code]} instance outside of the package.
class HH {
public String msg="outside the package the public variable is accessed";
String info;
public void disp()
{
System.out.println("outside the package the public variable is accessed ");
System.out.println(info);
}
Public HH (String info)
{
this.info=info;
}
}
//save by PublicEx1.java
package com. Javalang;
import com.java. HH;
public class PublicEx2 {
public static void main (String [] s) {
HHh=new HH ("outside the package trying to create the instance of public constructor ");
System.out.println(h.msg);
h. disp();
}
}
Output:
outside the class the public variable is accessed
outside the class the public variable is accessed
outside the class trying to create the instance of public constructor
Example 3:
An illustration of how the public access modifier can be used to identify whether a public method is overridden by a subclass.
class HH
{
public void msgs ()
{
System.out.println("try to do it");
}
}
class PublicEx3 extends HH {
public void msgs ()
{
System.out.println("accessing the overridden method");
}
public static void main (String [] s) {
PublicEx3 h=new PublicEx3();
h. msgs();
}
}
Output:
accessing the overridden method
Example 4:
An illustration of how the private access modifier can be used to identify whether a public method is overridden by a subclass.
class AA
{
public void msgs ()
{
System.out.println("Try it access");
}
}
class PublicEx4 extends AA {
private void msgs ()
{
System.out.println("accessing the overridden method");
}
public static void main (String[] s) {
PublicEx4 p=new PublicEx4();
p. msgs();
}
}
Output:
Error: Unresolved compilation problem
Example 5:
An illustration of using the default access modifier to check whether the public function is overridden by the subclass.
class AA
{
public void msgs ()
{
System.out.println("Try it access");
}
}
class PublicEx4 extends AA {
void msgs ()
{
System.out.println("accessing the overridden method");
}
public static void main (String[] s) {
PublicEx4 p=new PublicEx4();
p. msgs();
}
}
Output:
Error: Unresolved compilation problem
Example 6:
An illustration of how the protected access modifier can be used to identify whether a public method is overridden by a subclass.
class AA
{
public void msgs ()
{
System.out.println("Try it access");
}
}
class PublicEx4 extends AA {
protected void msgs ()
{
System.out.println("accessing the overridden method");
}
public static void main (String[] s) {
PublicEx4 p=new PublicEx4();
p. msgs();
}
}
Output:
Error: Unresolved compilation problem