JAVA ENCAPSULATION

 

 

Encapsulation  in  Java  is  a  core  concept   in   object-oriented  programming (OOP ) concept   which  combine  data  and   the  functions  that  work  on  data  into  a  single  unit  like  class.  Encapsulation  is  also  known  as    data  hiding  because  it  restricts  the  direct  access  to the  internal  state  of  an  object  by  protecting  data  from  unauthorized  access .

 

 

 

 

HOW  ENCAPSULATION  IS  ACHIEVED  IN  JAVA

 

 

 

Encapsulation  is   achieved  in  Java  by  using :

  • Private  data  members
  • Public  getter  and  setter  methods

Key Rules  for  Java   Encapsulation:

 

 

  • Declare  variable or  data  as  private :   By  declaring  data  as  a  private  prevents  any  outside  class  from  accessing  the  data  fields  directly.
  • Provide  public  getter  and  setter  methods :   These  methods  in  encapsulated   class  provide  controlled   access  to  the  private  variables .

                 Getter  methods  retrieve  the  value  of  a  variable (e.g. , getValue() ).

                Setter  methods  set  or  update  the  value   of  a  variable (e.g., setAmount(” $500″ ).

 

 

 

 

 

 

Example :

 

 

public  class   Gift  {

 

    //Declaring  data  variables  as  private

 

private  String   giftName;

private  int   score;

 

//public   getter  method  for  giftName

 

public  String   getgiftName(){

             return   giftName;

 

  }

 

 

//public  setter  method  for  giftName

 

 public  void   setgiftName ( String   newName){

    this.giftName=newName;

}

 

//public  getter  method  for  score  

 

public  int  getScore () {

return   score;

 }

 

//public  setter  method  for  score  with  validation

 

public  void  setScore ( int  newScore) {

 

     if ( newScore>90 && newScore<101) {

         this.score  =  newScore ;

       }   else   {

System.out.println(“Invalid  score “);

  }

 

   }

 

 

 

 

 

 

public   class   DemoEncapsulation  {

 

          public  static  void  main (String  args[] ) {

         Gift    gif  =new  Gift  ();

 

       //Set  values  using  setter  methods

 

    gif.setgiftName(” $500″);

   gif.setScore(95);

 

   // Get  values   using  getter  methods   and  print

 

System.out.println(” Giftname : “ + gif.getgiftName());

 

System.out.println(“Score : “ + gif.getScore());

 

    }

 

    }

 

 

 

 

 

 

 

 

OUTPUT:

 

Giftname :  $500

Score : 95

 

 

 

 

 

Advantages  of   Encapsulation :

 

  • Easy  Maintainability :  By  using  encapsulation ,  changes  to   internal  implementation  can  be  made  without  affecting  external  code  that  uses  the  class .
  • Data  Hiding :    Java  Encapsulation  restricts  direct  access  to  class  variables , protecting  sensitive  data  from  unauthorized  access .
  • Enhanced  Security :    Encapsulation  allows  control  and  validation  over  data , preventing  invalid  values  to  be  set.
  • Code  Reusability :  Java  Encapsulated  classes  can  be  reused  in  different  programs  without  exposing   internal  logic .
  • Better    Modularity :   Encapsulation  enhance  organized , modular  code  by  keeping  data  and  methods  together  within  a  class .

 

 

 

 

Disadvantages  of  Encapsulation :

 

 

  • Performance  Overhead :  In  Java  Encapsulated  class  by  accessing   data  through  methods  instead  of  directly   accessing  can  cause a  minor   performance  cost, basically  in  performance-critical  applications .
  • Increased  Code  Complexity :  In  Java  Encapsulation  class  by  writing  getter  and  setter  methods   for  every  variable   can  make  the  code  longer  and  sometimes  more  complex .
  • Less  Flexibility  in  Some  Cases :    In  Java  encapsulated  class ,  over  restricting  access  to  class  members  may  limit  the  ability  of  other  classes  to  extend  or  use  the  class  efficiently .

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *