JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Encapsulation in Java

Encapsulation means the ‘encapsulating’ or ‘wrapping up’ of data. This is actually a mechanism that binds data together.

Encapsulation in Java is a mechanism to wrap up variables and methods together as a single unit. It is the process of hiding information details and protecting data and behavior of the object. It is one of the four important OOP concepts. The encapsulate class is easy to test, so it is also better for unit testing.

When encapsulation is implemented, only the variables inside the class can access it. No class outside the current class can access the variables inside it.

Example of Encapsulation:

class Simple {
    private String course;
    public String getCourse() {
        return course;
    }
    public void setCourse(String str) {
        this.course = str;
    }
}

public class Encapsulation {
    public static void main(String[] args) {
        Simple obj = new Simple();

        //set data 
        obj.setCourse("Java");
        
        //get data
        System.out.println(obj.getCourse());

    }
}

Output:

java

Why Do We Need Encapsulation

There are various reasons as to why encapsulation is essential in Java:

1. Encapsulation allows us to modify the code or A part of the code without having to change any other functions or code.

2. Encapsulation makes our applications simpler.

3. We can modify the code based on the requirements using encapsulation.

4. Encapsulation controls how we access data.

Getter and Setter in Java

Getter and Setter in Java are two conventional methods used to retrieve and update values of a variable. They are mainly used to create, modify, delete and view the variable values. The setter method is used for updating values and the getter method is used for reading or retrieving the values. They are also known as an accessor and mutator.

Advantages of Encapsulation in Java

1. It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.

2. Encapsulation is binding the data with its related functionalities. Here functionalities mean methods and data means variables

3. So we keep variable and methods in one place. That place is class. Class is the base for encapsulation.

4. Encapsulation also improves the re-usability and easy to change with new requirements.

5. Encapsulated code is easy to test for unit testing.