Java code,java,Example,Tutorials,Syntax,Hibernate,Jsp, Servlet, FileHeadling,Java or Core Java Tutorial or Java Programming Tutorial is a widely used robust technology. learning of java from basic questions like what is java tutorial, core java, where it is used, what type of applications are created in java and why use java.

Search This Blog

Wednesday, 6 July 2016

What is java Modifiers?


AccessModifier

What is Modifier in java and List the Categories of Modifier?


There are two types of modifiers in java: access modifiers and non-access modifiers.
The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
There are 4 types of java access modifiers:
  1. private
  2. default
  3. protected
  4. public



private access modifier

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

                         class vipul{ 
                     private vipul()
                           {
                            }//private constructor 
                           void msg()
                               {
                                     System.out.println("Hello java");
                                 }
                              }
                   public class Simple 
                              {
                                   public static void main(String args[])
                                          {
                                              vipul obj=new vipul();//Compile Time Error  
                                          }
                               }  

default access modifier

      we have created two packages pack and mpack. We are accessing the VIPUL class from outside its package, since VIPUL class is not public, so it cannot be accessed from outside the package.

                     //save by VIPUL.java 
                                       package pack;
                                                      class VIPUL
                                                  {   
                                                                   void msg()
                                                           {
                                                                System.out.println("Hello");
                                                               }
                                                          }  


            //save by Agravat.java 
                         package mpack;
                            import pack.*;
                                     class Agravat
                                            {
                                                 public static void main(String args[]){     
                                                       VIPUL obj = new VIPUL();//Compile Time Error                                                                      obj.msg();//Compile Time Error   
                                                           }
                                                    }  
the scope of class VIPUL and its method msg() is default so it cannot be accessed from outside the package.

protected access modifier

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



  //save by VIPUL.java
  package pack; 
               public class VIPUL
                        {
                        protected void msg()
                                {
                                        System.out.println("Hello");
                                   }
                        }  
                               
                      we have created the two packages pack and mpack. The VIPUL class of pack package is public, so can be accessed from outside the package. But msg() method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.

               //save by Agravt.java 
               package mpack;
                     import pack.*;
                         class Agravat extends VIPUL
                                     {
                                      public static void main(String args[])
                                             {
                                                   Agravat obj = new Agravat();    
                                                          obj.msg();   
                                                }
                                          }  
 print "Hello"..........

public access modifier

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

 //save by VIPUL.java                    package pack;
                 
public class VPIUL
                           {
                                
public void msg()
                                      {
                                        System.out.println(
"Hello");
                                           }
                                  
}
  
//save by Agravat.java                package mpack;
              
import pack.*;                    class Agravat
                             {
                                   
  public static void main(String args[])
                                              {
                                             
   VIPUL obj = new  VIPUL();
                                                   
   obj.msg();
                                               
  }
                                          
}

javacode
  
Share:

Agravat &Google

Total Pageviews

Blog Archive

vipul c.Agravat . Powered by Blogger.

myjava

myjava
java

myimg

myimg
good

Translate

Recent Posts

Unordered List

Definition List

Pages

Theme Support