Wednesday, October 2, 2013

Access Specifiers in JAVA

Access Specifiers in JAVA :-


Class Package Subclass in Same Package Subclass in Different Package World
public Y Y Y Y Y
protected Y Y Y Y
default Y Y Y
private Y


Public is accessible everywhere.
Protected can not be assessed in outside the subclasses or the same package.
Default can not be accessed in subclasses in different packages.
Private can be accessed in the class only.

The difference between the protected and default is that the protected can be accessed in subclasses in different class but default can not be.

Example

package com.anshul.projects.iwts.tutor.accessspecifier;

public class MyClass {
    public final static int public_variable= 5;
    protected final static int protected_variable = 5;
    final static int default_variable = 5;
    private final static int private_variable = 5;
  
    public void public_method(){
        System.out.println("public_method");
    }
  
    protected void protected_method(){
        System.out.println("protected_method");
    }
  
    void default_method(){
        System.out.println("default_method");
    }
  
    private void private_method(){
        System.out.println("private_method");
    }


   public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }
}


package com.anshul.projects.iwts.tutor.accessspecifier;

public class Package {
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }}

package com.anshul.projects.iwts.tutor.accessspecifier;

public class SubClass extends com.anshul.projects.iwts.tutor.accessspecifier.MyClass{
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }}

package com.anshul.projects.iwts.tutor.accessspecifier.subpackage;
import com.anshul.projects.iwts.tutor.accessspecifier.MyClass;

public class OtherPackageSubClass extends MyClass{
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }  }

package com.anshul.projects.iwts.tutor.accessspecifier.subpackage;
import com.anshul.projects.iwts.tutor.accessspecifier.MyClass;

public class World {
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }}

All the red highlighted lines will be giving compile time error "Method" or "Variable" is not visible



No comments:

Post a Comment

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...