Hello readers...Java is the latest article series I started. So, this is the 5th article I write based on Java. I discussed Java Classes and Objects previously. It is the beginning of Object Oriented Programming series. If you missed it, visit the below link and read to grab the idea.

There are 4 main OOP concepts in any Object Oriented Programming language. They are;
  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Among these, today I will discuss about Encapsulation...Let's start!

What is Encapsulation?

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. This means simply hiding some functionalities or attributes from outsiders. Outsiders refers to the other classes. These hidden properties and behaviors can be only accessed through some methods. No one can access them directly. Still the other classes can use the encapsulated classes, but they can not find the in-going mechanism of the class to some extent.

How to achieve Encapsulation in Java?
  • Make the properties private
  • Implement public getter and setter methods to access properties

Then, what are getters and setters?
  • Setters are the methods responsible to set variable values.
  • Getters are the methods responsible to return variable values.
Now I will show you an example. 

class Student{
    // Attribute declarations
    private String name;
    private int age;
    // Setters
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    // Getters
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
}

class Demo{
    public static void main(String args[]){
        Student s1=new Student();
        s1.setName("Salitha");
        s1.setAge(23);
        System.out.println("Name of the student : " + s1.getName());
        System.out.println("Age of the student : " + s1.getAge());
    }
}

Output:


Since age and name have private access modifier, they can not be accessed outside the Student class. It means, outer classes don't know about age and name. So, as I told before we need getters and setters to get access to them. The two methods called setAge and setName are getting the values and assigning the values to the attributes of the currently created object. This reference is done by this keyword. Then the two getters return the age and name. 

Next we create an object using Student class. If we need to access age variable or name, can we type just s1.age or s1.name? NO..Age is a private attribute..So it can not be retrieved directly from outside. They should be taken through the setters and getters. For both setters, I have passed the parameter values to the setters. So. they have done the job for me.

Another example:

class Box{
    // Attribute declarations
    private int height;
    private int weight;
    private int length;
    // Setter
    public void setValues(int height, int weight, int length){
        this.height = height;
        this.weight = weight;
        this.length = length;
    }
    // Getters
    public int getHeight(){
        return height;
    }
    public int getWeight(){
        return weight;
    }
    public int getLength(){
        return length;
    }
    public int printVolume(){
        //Local variable
        int volume;
        volume = height*weight*length;
        return volume;
    }
}

class Demo{
    public static void main(String args[]){
        Box b1 = new Box();
        b1.setValues(10, 20, 30);
        System.out.println("Height of the box : " + b1.getHeight());
        System.out.println("Weight of the box : " + b1.getWeight());
        System.out.println("Length of the box : " + b1.getLength());
        System.out.println("Volume of the box : " + b1.printVolume());
    }
}

Output:

Here, I used one and only setter to set values. All 3 attributes are set in the setValues method. Then I used 3 getters to return the values of attributes. Finally I implemented a method to return the volume of a Box object using these attributes. In my main method, I created a Box object and called the setter, getters and printVolume methods. That's all...

So, this is the way to implement Encapsulation in your code. When the word OOP is heard, someone get scared of it. It's true this is not an easy section. But think it simply. This is the way I understood Encapsulation in Java. That's why I thought to share this with you!

My next Java articles will discuss more OOP concepts..Till then Good Bye!


0 Comments