Java – Object Oriented Programming Concepts – Encapsulation


What is object oriented programming language ?

An object-oriented programming language (OOPL) is a high-level programming language based on the object-oriented model.

Why java is called an object oriented programming language ?

Java is object oriented language because it uses objects for its existence. 

Everything is object in java and everything can called using objects. 

OOPs concepts in java

oops concept is a design of computer programs where everything is an object that interact with one another.

What are the concepts of object oriented programming ?

There are four main OOP concepts, they are

1. Encapsulation

2. Abstraction

3. Inheritance

4. Interface

5. Polymorphism

Java – Encapsulation :


Encapsulation is one of the fundamental OOP concepts.

What is encapsulate meaning ?

Encapsulate means enclosing something in a capsule.

Its also termed as enclosing all the things and forming a closed container.

What is encapsulation in java ?

Encapsulation is the inclusion of one thing within another thing so that the included thing is not apparent. 

What is decapsulation ?

Decapsulation is the removal or the making apparent a thing which is previously encapsulated.

Why do we use encapsulation in java ?

Encapsulation is to hide the implementation details from users. 

for example, If a data member variable is private it means it can only be accessed within the same class. 

No outside class can access private data member variable of other class. 

So encapsulation process is known as data hiding process.

Advantages of encapsulation :

  1. It helps in binding the data variables and the member functions of a class.

2. It helps in hiding the data variables of a class from an illegal direct access.

3. It helps us to make a flexible code which is easy to change and maintain.

How to implement encapsulation in java ?

There are Two ways to implement encapsulation.

1) Make the instance variables private so that it cannot be accessed from outside the class directly.

we can only set and get values of these variables through the methods of the class.

2) Have getter and setter methods in the class to set and get the values of the fields.

Example 1 :

public class ClassA{
 public static void main(String args[])
 {

  ClassB obj = new ClassB();
  System.out.println("Step 1 : Initially setting name,age and sex - started");
  obj.setName("Abdul Kalam");
  obj.setAge(83);
  obj.setSex("Male");
  System.out.println("Name = "+obj.getName());
  System.out.println("Age = "+obj.getAge());
  System.out.println("Sex = "+obj.getSex());
  System.out.println("Step 1 : Initially setting name,age and sex -  ended");
  System.out.println("Step 2 : Initially setting name,age and sex - started");
  obj.setName("Mother Teresa");
  obj.setAge(87);
  obj.setSex("Female");
  System.out.println("Name = "+obj.getName());
  System.out.println("Age = "+obj.getAge());
  System.out.println("Sex = "+obj.getSex());
  System.out.println("Step 2 : Initially setting name,age and sex -  ended");

 }

}
class ClassB{
 private String name;
 private int age;
 private String sex;

 public String getName()
 {
  return name;
 }

 public int getAge()
 {
  return age;
 }

 public String getSex()
 {
  return sex;
 }

 public void setName(String name)
 {
  this.name = name;
 }

 public void setAge(int age)
 {
  this.age = age;
 }

 public void setSex(String sex)
 {
  this.sex = sex;
 }
}

Output :

Step 1 : Initially setting name,age and sex – started


Name = Abdul Kalam

Age = 83

Sex = Male

Step 1 : Initially setting name,age and sex –  ended

Step 2 : Initially setting name,age and sex – started

Name = Mother Teresa

Age = 87

Sex = Female

Step 2 : Initially setting name,age and sex –  ended