Friday, 19 December 2014

What is access modifiers and access specifiers in java

Access Modifiers:

There are two types of modifiers access modifier and non-access modifier.
These  access modifiers specifies accessibility (scope) of a datamember, method,constructor or class. So this is also know as access specifiers.

These are 4 types:

>1. private
>2. default
>3. protected
>4. public

There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.
Here, we will see only access modifiers.

>1. private:

The private access modifier is accessible only within the class.

Example of private access modifer(variable,method):

In this example,we have created two classes Test and Student.Test class contains private
data member and private method.We are accessing these private members from outside the class,
so there is compile time error.

class Test{
private int data=20 ;
private void run(){   System.out.println("hello private");}
  }
public class Student{

public static void main(String []args){
Student st=new Student();
System.out.println(st.data);
st.run();
    }
}

Output: compile time error

private constructor:

If you make any class constructor as private, you cannot create the instance of that class
from outside that class.

Example of private constructor:

class Test{
private Test(){}   //private constructor
void run(){  System.out.println("hello private constructor");}
}
public class Student{

public static void main(String []args){
Student st=new Student();
    }
}

Output: compile time error

Note: A class cannot be private or protected except nested class.


>2. default:

If you don't use any modifier,it is treated as default modifier bydefault.The default modifier is accessible only within the package.

Example of default access modifier:

In this example, we have created two packages com and vishwa.We are accessing the Test class from
outside its package,since Test class is not public,so it cannot be accessed from outside the package

Test.java

package com;
class Test{
void run(){
System.out.println("test running");
  }
}


Student.java

package vishwa;
import pack.*;
class Student{

public static void main(String []args){
Test t = new Test();
t.run();
   }
}

Output: compile time error

In the above example,the scope of class Test and its method run() is default so it cannot be accessed
from outside the package.

>3. protected:

The protected access modifier is accessible within package and outside the package by only through
inheritance. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.

Example of protected access modifier:

In this example, we have created the two packages com and vishwa.The Test class of com package is
public, so can be accessed from outside the package.But run method of this package is declared as
protected,so it can be accessed from outside the class only through inheritance.

Test.java

package com;
public class Test{
protected void run(){
System.out.println("test running");
  }
}

Student.java

package vishwa;
import com.*;
class Student extends Test{

public static void main(String []args){
Student st = new Student();
st.run();
    }
}

Output: test running


>4. public:

The public access modifier is accessible everywhere.It has the maximum scope among all other modifiers.

Example of public access modifier:

Test.java

package com;
public class Test{
public void run(){
System.out.println("testing");
   }
}


Student.java

package vishwa;
import com.*;
class Student{

public static void main(String []args){
Student st = new Student();
st.run();
   }
}

Output: testing


Note: If you are overriding any method, overriden method (declared in subclass) must not be         more restrictive.

For Example:

class Test{
protected void run() {
System.out.println("testing");}
}
public class Simple extends A{
void run(){System.out.println("testing ");}

public static void main(String []args){
Student st=new Student();
st.run();
   }
}

Output: compile time error

Note: Because the default modifier is more restrictive than protected.That is why  compile time error comes here.




No comments:

Post a Comment