Encapsulation in the Form of Java Programming

Introduction :

In this article we are going to describe how we can use encapsulation in programming terms with Java. So we first describe the meaning of this very popular concept; encapsulation. And after this we give the example of how we implement this concept in the Java language.

Encapsulation :

Encapsulation is one of the four fundamental concepts (Inheritance, Polymorphism, Encapsulation and Abstraction) of OOP. In general Encapsulation refers to data hiding. Encapsulation in Java is a technique which is used to visible at different places and the Java language itself provides many constructs to encapsulate members. You can completely encapsulate a member be it a variable or method in Java by using the private keyword and you can even achieve a lesser degree of encapsulation in Java by using other access modifiers like protected or public.

encasulation1.gif

Note: The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Benefits of Encapsulation :

  • You can achieve data hiding throw encapsulation technique.
  • The fields of a class can be made read-only or write-only.
  • The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.
  • A class can have total control over what is stored in its fields.
     

How Can Achieve Encapsulation :

In the following example you can see how public methods are the access points to this class's fields from the outside Java world. Normally these methods are referred to as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.

Example :

Class: public  EncapExample

public class EncapExample

  {

   private String collageName;

   private String colId;

   private int noStudent;

 

   public String getcollageName(){

      return collageName;

   }

 

   public String getcolId(){

      return colId;

   }

 

   public int getnoStudent(){

      return noStudent;

   }

 

  public void setcollageName(String newName){

       collageName= newName;

   }

 

   public void setcolId( String newId){

      colId = newId;

   }

   public void setnoStudent(int no)

   {

   noStudent=no;

   }

}

 

Class : TestEncasulation

public class TestEncasulation

{

   public static void main(String args[]){

      EncapExample encp = new EncapExample();

      encp.setcollageName("I.M.S Ghaziabad");

      encp.setcolId("ims36");

        encp.setnoStudent(2000);

     System.out.println("Collage Name : " + encp.getcollageName());

       System.out.println("Collage ID : " + encp.getcolId());

       System.out.println("total no of student : " + encp.getnoStudent());

       

    }

}

 

OUTPUT :

encaps2.jpg 

Up Next
    Ebook Download
    View all
    Learn
    View all