Introducing Java Encapsulation Concept

 


Encapsulation

Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

To achieve encapsulation in Java −

1.) Declare the variables of a class as private.

2.) Provide public setter and getter methods to modify and view the variables values

public class EncapTest {
   private String name;
   private String idNum;
   private int age;

   public int getAge() {
      return age;
   }

   public String getName() {
      return name;
   }

   public String getIdNum() {
      return idNum;
   }

   public void setAge( int newAge) {
      age = newAge;
   }

   public void setName(String newName) {
      name = newName;
   }

   public void setIdNum( String newId) {
      idNum = newId;
   }
}

Benefits of Encapsulation -

1.) The fields of a class can be made read-only or write-only.

2.) A class can have total control over what is stored in its fields.

Comments

Popular posts from this blog

Introducing Java Interfaces Concept

Introducing Java Method Overriding And Super Keyword