Java OOP Concepts – Encapsulation

Hello guys, I am finally back after a long break. Sorry for not being able to publish articles regularly.

Today, I am going to start a new article series regarding Java OOP Concepts. To start this off, I would like to discuss Encapsulation in this article.

Encapsulation is one of the core concepts when it comes to Java OOP (Object Oriented Programming) concepts.

What is Encapsulation?

Encapsulation is a mechanism we use to achieve data hiding and security. When we implement encapsulation in a certain class, the data related to that class only becomes accessible through methods. We can’t directly access or update the data once encapsulation is implemented.

Why should we implement Encapsulation in our code?

  • It makes our code more flexible and easy to change.
  • It helps us to make our code secure
  • We can make the class read-only if we don’t implement setter methods
  • We can make the class write-only if we don’t implement getter methods

How can I implement Encapsulation?

Let me write and explain the code as I write it.

public class Circle{
    
    //Defining Variables
    private double radius = 5.0;
    private String color = "Blue";

}

Here I created a Circle class with two attribures, radius and color. Nothing complicated. You may have recognized that I’ve used the private keyword to limit the access to the variables.

Why do we use private keyword?

Let me update the above code quickly to explain the difference

public class Circle{
    
    //Defining Variables
    String notPrivate = "I am visible"; 
    private double radius = 5.0;
    private String color = "Blue";

}

And I’ve created Test class and I am trying to access these variables from there.

public class Test{

    public static void main(String [] args){

        Circle circle = new Circle();
        //Prints I am visible
        System.out.println("Here is the normal variable : " + circle.notPrivate); 
        //Compile Error
        System.out.println("Here is the private variable : " + circle.color );  
    }
}

When we try to access a private variable, JVM throws an error like this

Compile Error

The same error occurs if you try to update the variable directly like this.

circle.color = "Red";

Adding Getters

So, let me revert this to the earlier code. Now we know that we can’t directly access these private variables. What can we do now?

Yes. We can add getter methods to achieve this. Here is the updated Circle class.

public class Circle{
    
    //Defining Variables
    private double radius = 5.0;
    private String color = "Blue";

    //Defining Getters
    public double getRadius(){
        return radius;
    }

    public String getColor(){
        return color;
    }

}

We need to make these methods public so that they are accessible from other classes. Now we can use these getter methods to access our private variables.

public class Test{

    public static void main(String [] args){

        Circle circle = new Circle();
        System.out.println("Radius of the circle : " + circle.getRadius());
        System.out.println("Color of the circle : " + circle.getColor());
    }
}

This prints out the following values to the console.

Adding Setters

So, now we have created a read-only class. We can read the data in the class, but we still can’t update the data. We need to add setters to update the data.

public class Circle{
    
    //Defining Variables
    private double radius = 5.0;
    private String color = "Blue";

    //Defining Getters
    public double getRadius(){
        return radius;
    }

    public String getColor(){
        return color;
    }

    //Defining Setters
    public void setRadius(double radius){
        this.radius = radius;
    }

    public void setColor(String color){
        this.color = color;
    }

}

Let’s try to update the values using these methods

public class Test{

    public static void main(String [] args){

        Circle circle = new Circle();

        //Testing Getters
        System.out.println("Radius of the circle : " + circle.getRadius());
        System.out.println("Color of the circle : " + circle.getColor());

        //Testing Setters
        circle.setRadius(10.0);
        circle.setColor("Red");

        System.out.println("Radius of the circle : " + circle.getRadius());
        System.out.println("Color of the circle : " + circle.getColor());

    }
}

This code prints out the following output to the console.

Alright, now we have fully implemented encapsulation in our code.

We can increase the security further if we implement abstraction concept. I will discuss abstraction in a later article.


Thank you for reading my article. I hope you learned something valuable from it. If you did, drop a like and follow my blog to get notifications when I publish new articles. I write articles about coding, cricket and things that I find interesting. I try to publish articles every other day. Have a nice day ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: