Java Access Modifiers
            SCJP/OCJP exam objectives – 1.1,1.3,1.4




                                              By,
                                       Srinivas Reddy.S
www.JAVA9S.com
Access Modifiers - Importance


         GoogleSearch
             int pageRank;
               int index;
    getSearchResults(String query){}
   calculatePageRankAndIndexPages
                   (){}
Access Modifiers
•   public
•   protected
•   default
•   private
Access Modifiers - Importance
• Access modifiers helps to implement the
  concept of encapsulation in OOPs.
• Encapsulation helps to hide the data and
  behavior.
• Access Modifiers affect the accessibility at
  two levels :
  • Class
  • Members(methods and instance variables)
Access Modifiers – at class level
public
• When a class is marked as public, it is visible to all
  other classes which are inside and outside the
  package. – Visible to everyone..

 package com.java9s.Engines;           package com.java9s.cars;
                                       import com.java9s.Engines.Engine;
 public class Engine{
          public int engine1000cc(){   public class Ford{
                   int rpmCount=…..;            void moveFast(){
                   return rpmCount;               Engine e = new Engine();
          }                                     int rpm = e.engine3000cc()
          public int engine3000cc(){            //move with speed related to
                   int rpmCount=…..;            //rpm
                   return rpmCount;             }
          }                            }
 }
public - class level
• A class marked as public is be accessible to all
  classes in other packages, but should
  mention the import statement.
• There is no need for an import statement
  when a class inside the same package uses
  the class marked as public
default – class level
• Default access has no key word.
• When there is no access modifier declared,
  the access is default level access.
• The classes marked with default access are
  visible only to the classes inside the same
  package.
default – class level
package com.java9s.bank;
class InterestRates{                                When there is no
      double creditCardInterest(){                  access modifier specified
      …….;                                          - The access is default level
      }

      double homeLoanInterest(){
      ……;
      }                  package com.java9s.bank;
}                        public class customerBanking{
                             double calculateHomeLoan(){
                             InterestRates i = new InterestRates();
                             double interest = i.creditCardInterest();
                             //calculate the home loan of customer
                             return x;
                              }
                         }
default – class level
package com.java9s.bank;
class InterestRates{
        double creditCardInterest(){
        …….;
        }

          double homeLoadInterest(){
          ……;
          }                       package com.java9s.Customerbank;
}                                 public class customerBanking{
                                      double calculateHomeLoan(){
                                      InterestRates i = new InterestRates();
 The accessing class is not           double interest = i.creditCardInterest();
 in the same package. So, the class
                                      //calculate the home loan of customer
 With default will not be visible
                                      return x;
                                       }
                                  }
protected and private – class level
***protected and private access modifiers are
  not applicable to the class level
  declaration***
Access modifiers – member level
(methods and instance variables)
public – member level
• A member with public access is visible to all
  the classes inside and outside package.
• The class in which the member exists should
  be is visible to the accessing class.
public – member level
package com.java9s.Engines;           package com.java9s.cars;
                                      import com.java9s.Engines.Engine;
public class Engine{
         public int engine1000cc(){   public class Ford{
                  int rpmCount=…..;            void moveFast(){
                  return rpmCount;               Engine e = new Engine();
         }                                     int rpm = e.engine3000cc()
         public int engine3000cc(){            //move with speed related to
                  int rpmCount=…..;            //rpm
                  return rpmCount;             }
         }                            }
}

         •Check if the class itself is accessible
         •Check if the import statement is used to import the class(if
                   accessed from other package)
         •Finally- check if the member is accessible
public – member level
package com.java9s.Engines;           package com.java9s.cars;
                                      import com.java9s.Engines.Engine;
class Engine{
         public int engine1000cc(){   public class Ford{
                  int rpmCount=…..;            void moveFast(){
                  return rpmCount;               Engine e = new Engine();
         }                                     int rpm = e.engine3000cc()
         public int engine3000cc(){            //move with speed related to
                  int rpmCount=…..;            //rpm
                  return rpmCount;             }
         }                            }
}


     The class engine is having default access and will not be visible to Ford
default – member level
• A member marked with default access will be
  visible to the classes that are in the same
  package.
default – member level
package com.java9s.bank;
class InterestRates{
      double creditCardInterest(){
      …….;
      }

      double homeLoadInterest(){
      ……;
      }                  package com.java9s.bank;
}                        public class customerBanking{
                             double calculateHomeLoan(){
                             InterestRates i = new InterestRates();
                             double interest = i.creditCardInterest();
                             //calculate the home loan of customer
                             return x;
                              }
                         }
protected – member level
• A member marked as protected is visible to
  all classes in the same package(Just like
  default).
• protected members are also accessible to
  classes outside the package, but the
  accessing class should be a subclass of the
  member class.
protected- member level
                                      package com.java9s.cars;
 package com.java9s.cars;
 public class Car{                    public class Benz{
   protected int speed;                 int move(){
 }                                          Car c = new Car();
package com.java9s.fastCars;                return c.speed;
public class Ferrari extends Car{           }
         int moveFast(){
         return super.speed;          }
         }
   int move(){                      Note: The protected members can be
         Car c= new Car();          accessed only by the subclasses in other
         c.speed;                   packages and can invoke the members
                                    only through inheritance mode.
         }
                                    Not by creating an instance.
}
private – member level
• When a member is marked as private, it is
  only visible to other members inside the
  same class.
• Other classes inside and outside the package
  will not be able to access the private
  members of a class.
private – member level

 public class Car{
   private String keyCode;
 }


public class Theif{          keyCode variable is not
      void steal(){          visible outside the class Car

      Car c = new Car();
      c.keyCode;
      }
}
protected and private – class level
***protected and private access modifiers are
  not applicable to the class level
  declaration***


                 ???
Access Modifiers - summary
Visibility                      public   protected     default   private
From the same class             Yes      Yes           Yes       Yes

From a class in same package    Yes      Yes           Yes       No



From a class from outside the   Yes      No            No        No
package



From a subclass outside the     Yes      Yes(Only      No        No
package                                  through
                                         inheritance
                                         mode)
From a subclass in the same     Yes      Yes           Yes       No
package
Access Modifiers – Local Variables
public class Calculator{
   public int addition(int a, int b){
  int c = a+b;
  return c;
                      C is a local variable and only lives till the method
  }                   Executes.

}

No access modifiers should be applied for Local variables
Thank you
   Follow me on to get more updates on latest video posts
   Subscribe on

   http://www.youtube.com/user/java9s
   Twitter :   @java9s

   facebook: www.facebook.com/java9s




www.JAVA9S.com

Java access modifiers

  • 1.
    Java Access Modifiers SCJP/OCJP exam objectives – 1.1,1.3,1.4 By, Srinivas Reddy.S www.JAVA9S.com
  • 2.
    Access Modifiers -Importance GoogleSearch int pageRank; int index; getSearchResults(String query){} calculatePageRankAndIndexPages (){}
  • 3.
    Access Modifiers • public • protected • default • private
  • 4.
    Access Modifiers -Importance • Access modifiers helps to implement the concept of encapsulation in OOPs. • Encapsulation helps to hide the data and behavior. • Access Modifiers affect the accessibility at two levels : • Class • Members(methods and instance variables)
  • 5.
    Access Modifiers –at class level
  • 6.
    public • When aclass is marked as public, it is visible to all other classes which are inside and outside the package. – Visible to everyone.. package com.java9s.Engines; package com.java9s.cars; import com.java9s.Engines.Engine; public class Engine{ public int engine1000cc(){ public class Ford{ int rpmCount=…..; void moveFast(){ return rpmCount; Engine e = new Engine(); } int rpm = e.engine3000cc() public int engine3000cc(){ //move with speed related to int rpmCount=…..; //rpm return rpmCount; } } } }
  • 7.
    public - classlevel • A class marked as public is be accessible to all classes in other packages, but should mention the import statement. • There is no need for an import statement when a class inside the same package uses the class marked as public
  • 8.
    default – classlevel • Default access has no key word. • When there is no access modifier declared, the access is default level access. • The classes marked with default access are visible only to the classes inside the same package.
  • 9.
    default – classlevel package com.java9s.bank; class InterestRates{ When there is no double creditCardInterest(){ access modifier specified …….; - The access is default level } double homeLoanInterest(){ ……; } package com.java9s.bank; } public class customerBanking{ double calculateHomeLoan(){ InterestRates i = new InterestRates(); double interest = i.creditCardInterest(); //calculate the home loan of customer return x; } }
  • 10.
    default – classlevel package com.java9s.bank; class InterestRates{ double creditCardInterest(){ …….; } double homeLoadInterest(){ ……; } package com.java9s.Customerbank; } public class customerBanking{ double calculateHomeLoan(){ InterestRates i = new InterestRates(); The accessing class is not double interest = i.creditCardInterest(); in the same package. So, the class //calculate the home loan of customer With default will not be visible return x; } }
  • 11.
    protected and private– class level ***protected and private access modifiers are not applicable to the class level declaration***
  • 12.
    Access modifiers –member level (methods and instance variables)
  • 13.
    public – memberlevel • A member with public access is visible to all the classes inside and outside package. • The class in which the member exists should be is visible to the accessing class.
  • 14.
    public – memberlevel package com.java9s.Engines; package com.java9s.cars; import com.java9s.Engines.Engine; public class Engine{ public int engine1000cc(){ public class Ford{ int rpmCount=…..; void moveFast(){ return rpmCount; Engine e = new Engine(); } int rpm = e.engine3000cc() public int engine3000cc(){ //move with speed related to int rpmCount=…..; //rpm return rpmCount; } } } } •Check if the class itself is accessible •Check if the import statement is used to import the class(if accessed from other package) •Finally- check if the member is accessible
  • 15.
    public – memberlevel package com.java9s.Engines; package com.java9s.cars; import com.java9s.Engines.Engine; class Engine{ public int engine1000cc(){ public class Ford{ int rpmCount=…..; void moveFast(){ return rpmCount; Engine e = new Engine(); } int rpm = e.engine3000cc() public int engine3000cc(){ //move with speed related to int rpmCount=…..; //rpm return rpmCount; } } } } The class engine is having default access and will not be visible to Ford
  • 16.
    default – memberlevel • A member marked with default access will be visible to the classes that are in the same package.
  • 17.
    default – memberlevel package com.java9s.bank; class InterestRates{ double creditCardInterest(){ …….; } double homeLoadInterest(){ ……; } package com.java9s.bank; } public class customerBanking{ double calculateHomeLoan(){ InterestRates i = new InterestRates(); double interest = i.creditCardInterest(); //calculate the home loan of customer return x; } }
  • 18.
    protected – memberlevel • A member marked as protected is visible to all classes in the same package(Just like default). • protected members are also accessible to classes outside the package, but the accessing class should be a subclass of the member class.
  • 19.
    protected- member level package com.java9s.cars; package com.java9s.cars; public class Car{ public class Benz{ protected int speed; int move(){ } Car c = new Car(); package com.java9s.fastCars; return c.speed; public class Ferrari extends Car{ } int moveFast(){ return super.speed; } } int move(){ Note: The protected members can be Car c= new Car(); accessed only by the subclasses in other c.speed; packages and can invoke the members only through inheritance mode. } Not by creating an instance. }
  • 20.
    private – memberlevel • When a member is marked as private, it is only visible to other members inside the same class. • Other classes inside and outside the package will not be able to access the private members of a class.
  • 21.
    private – memberlevel public class Car{ private String keyCode; } public class Theif{ keyCode variable is not void steal(){ visible outside the class Car Car c = new Car(); c.keyCode; } }
  • 22.
    protected and private– class level ***protected and private access modifiers are not applicable to the class level declaration*** ???
  • 23.
    Access Modifiers -summary Visibility public protected default private From the same class Yes Yes Yes Yes From a class in same package Yes Yes Yes No From a class from outside the Yes No No No package From a subclass outside the Yes Yes(Only No No package through inheritance mode) From a subclass in the same Yes Yes Yes No package
  • 24.
    Access Modifiers –Local Variables public class Calculator{ public int addition(int a, int b){ int c = a+b; return c; C is a local variable and only lives till the method } Executes. } No access modifiers should be applied for Local variables
  • 25.
    Thank you Follow me on to get more updates on latest video posts Subscribe on http://www.youtube.com/user/java9s Twitter : @java9s facebook: www.facebook.com/java9s www.JAVA9S.com