As its name ' public ' itself indicates that any member of the class declared as public will be visible and accessible ANYWHERE. We have two scope in java programs :
1. Class scope
  • Sub-class in the same package       --> Public is visible .
  • Sub-class in different package        --> Public is visible.
  • Another class in the same package --> Public is visible.
  • Same class in the same package     --> Public is visible.
2. Package Scope
  • Within the same package -- > Public is visible. 
  • Outside the package       ---> Public is visible. 
So public members are visible EVERYWHERE and can be accessible form ANYWHERE.

Where and how we can use public access modifier in java ?
--> Public can be used with class, interface, constructor and variables.

Public with class 
If we are not using any access modifier with the class name then by default it is declared as ' default ' and declaring default will restrict its accessibility to the same package only. So to make any class visible to the another package we have to use public with the class. For ex-

public class prog { }

The above class is now visible to the other packages as well. 

Public with constructor
By default constructor is using default keyword before its name whose visibility will be limited to that package only in which class is been defined. If we want to access that constructor through another class which is defined in another package then we need to define the constructor as public to have its visibility to the other packages as well in which that is been imported. For ex-

Let suppose there are two class A and B in different packages com and def respectively and we import class A using import statement in def package where class B is defined to access the class A constructor by creating the object of class A. Now we came across few cases :

1. EXPLICIT CONSTRUCTOR : - If in com package any explicit constructor is there which is by default ' default ' and then if we try to create class A object in class B then it will compilation error that " constructor is not visible ".

2. NO EXPLICIT CONSTRUCTOR :- If we are not defining any constructor explicitly then JVM will invoke by default constructor and place public keyword before the constructor and hence it won't give any compilation error.

3. PUBLIC EXPLICIT CONSTRUCTOR :-  And if we are declaring constructor explicitly as public then it work fine and won't give and any compilation error.

Public with variable
Like constructor we can also use public keyword with class variable but not with the local variables as their scope will be within the method or constructor block only. Non-public members give compilation error " not visible ". Defining public with the class variables means that it can be accessible in the class which is defined in other package. for ex-

Let suppose there are two class and B in different packages com and def respectively and we import class A using import statement in def package where class B is defined to access the class A variables by creating the object of class A. like -

A.java in com package
------------------------------
package com;

public class A {

public int z=30;   // public variable.
int y=20;         // Be default ' default ' variable. 

 public A(){

  System.out.println(y);
  System.out.println(z);
 }
 public static void main(String sush[]) { }

}

B.java in def package
------------------------------
package def;
import com.A;

public class B{

 public static void main(String sush[]) {

     B b = new B();  // Class B object.
     A a = new A();  // Class A object.

  System.out.println(a.z);  //compiler successfully as it is 
                              declared public in class A.

  System.out.println(a.y);  // As y is not public so 
                              compilation error.
 }
}

Public with interfaces
Using public keyword before the interface will make it visible to any classes in any packages. If we are not using any public then it will not be visible to other packages and hence no class can be able to implement that interface. Without using public to the interface if we try to import that that interface it will give compilation error as " not visible " . for ex-

Let suppose there are two java files and B in different packages com and def respectively where are A.java contain an interface and B.java contain a java class B. We are importing interface A using import statement in def package where class B is defined to implement that interface using implements keyword. for ex-

A.java in com package
------------------------------
package com;

public interface A{

 void display();  // It is By default public and abstract.
}

B.java in def package
------------------------------
package def;
import com.A;  //No compilation error as interface is public

public class B implements A{

 public void display(){
  System.out.println("Display() method");
 }

 public static void main(String sush[]) {

  B b = new B();
  b.display();  
 }
}

The above program compile and execute successfully as because we are importing public interface A.If we are not using any public keyword then it will give compilation error while importing interface as " not visible " .

Note: In a same program we cannot have both public interface and public class as because we are writing java file name as the public class name. So having two public in a single file compiler will instruct to have other public as the java file name.


0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic