Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers:

  1. Access Modifiers: default, public, protected, private
  2. Non-access Modifiers: final, abstract, strictfp We will be looking into more details about modifiers in the next section.

Modifiers are keywords that you add to those definitions to change their meanings. The Java language has a wide variety of modifiers.


Access Modifiers in JAVA

The access modifiers in java specifies accessibility of a data member method , constructor or class.There are four types of java access modifiers .

  1. Private
  2. Default
  3. Protected
  4. Public

Private Access Modifier:

Private access modifier is the most restrictive access level. Methods, Variables and Constructors that are declared private can only be accessed. outside the class if public getter methods are present in the class.

Example:

Class A 
{
Private int data =40;
Private void msg ()
{
System.out.Println(“Hello java “);
}
}
public class  simple
pulic static void main (String args [])
{
A  obj  =  new A();
System.out.Println(“obj.data “);
Obj.msg();
}
}

Default Access Modifier

If you don’t use any modifier , it is treated as default by default . The default modifier is accessible only with in package.

Example:

//save  by  A.java
Package pack;
Class A
{
Void msg()
{
System.out.println(“Hello”)
}
//save  by  B.java
Package my  pack;
Import  pack.*;
Class B
{
Public static  Void main(String  args[])
{
A  obj = new A();
Obj.msg();
}
}

Public Access Modifier

The public access modifiers is accessible every ehere. It has the widest scope among all other modifiers.

Example:

//save  by  A.java
Package  pack;
 Public class A
{
 Public  void msg()
{
System.out.println(“Hello”)
}
}

//save  by  B.java
Package my  pack;
Import  pack.*;
Class B  
{
Public static  Void main(String  args[])
{
A obj = newA();
Obj.msg();
}
}


Protected Access Modifier

It is possible within package and out side the package but through inheritance only. The protected modifier can be applied of the data members method and constructor it can’t applied in the class.

Example:

//save  by  A.java
Package pack;
 Public class A
{
 Protected void msg()
{
System.out.println(“Hello”)
}
//save  by  B.java
Package my  pack;
Import  pack.*;
Class B  extends A
{
Public static  Void main(String  args[])
{
B obj = new B();
Obj.msg();
}
}

Understanding all java acess modifiers

Access Modifiers Within class Outside package by sub class only Outside packages
PrivateYNNN
DefaultYyNN
ProtectedYyyN(But extend)
PublicYyyy

Java access modifiers with method overriding

If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.

class A{  
protected void msg(){System.out.println("Hello java");}  
}  	  
public class Simple extends A{  
void msg(){System.out.println("Hello java");}//C.T.Error  
public static void main(String args[]){  
Simple obj=new Simple();  
obj.msg();  
}  
}  

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.