Programming Java Encapsulation in Java | Properties of Object Oriented Programming...

Encapsulation in Java | Properties of Object Oriented Programming (OOP)

-

Class encapsulation and abstraction are like two flip sides of a same coin. Because they both are used for data hiding, but in different way. In this article we will talk about class encapsulation.

Class encapsulation is- using the private modifier in a class to hide its data from direct access. It enables users to access only those elements which are declared public. So encapsulation describe the ability of an object to hide its data from the outside world, and it is also one of the fundamental principle of  OOP.

Class Encapsulation

Therefore, the “OOP Property – encapsulation” refers that – any private field can only be accessed within the class itself. Not even any object of the class or even its child(s) has right to access or modify the private field.

java encapsulation
Figure: Encapsulation property of Java OOP.

For example the class: Shape.java

public class Shape {
private int area;
private int length;
  public void rectangle(int a){
  area = a*a;
  }
}

Here, the field length is declared as a private field. Which means the length field is only accessible within the Shape class itself. Not even its objects nor its children or subclasses are allowed to access it.

Class: Main.java

public class Main{
public static void main (string[] args){
int some_var;
Shape squire = new Shape();
squire.length = ..; //error: length has the private access in the Shape class only
some_var=squire.rectangle(5);
System.out.println(“Area of squire: ” + some_var);
}}

So, this way “restricting data access” using “private” modifier in a class refers to Encapsulation or class encapsulation in OOP. Read more about controlling access to members of a class, here »

Getter and Setter

Furthermore, Java provides a method “getter and setter” that gives user access to the private fields. If the creator of the Shape class defines getter,  getLength() and settersetLength() methods in the class, then user can access its private field “length” using them.

So, using encapsulation we can totally deny access to the private field either declaring the getLength() method as private, or by not defining getLength() method at all. More about getter, setter read here »

public class Shape {

private int area;
private int length;

public int getLength (){
    return this.length;
  }
public void setLength (int a) {
   this.length = a;
  }
public void rectangle (){
  area = length*length;
  System.out.println(“Area of rectangle: ” + area);
  }
}

Shape shape = new Shape();
shape.setLength(10); //this will set private field, Length = 10
int a = shape.getLength();
shape.rectangle(); //output: 100.

getter or get method makes the field (variable) readable, and setter or set method makes the field updatable. Or simply, setter sets value to the private field, and getter reads the setted value.

We can use both getter and setter or any one them or none of them in the program as per requirement.

Except getter and setter there is no other way to access private fields in the program for the language restriction. But, regardless of the language restriction, there is a way in Java to access private fields of a class. Which is java reflection feature. To know how to access private fields, and methods of a class using reflection feature, read here »

//This is the end of discussion on general concept of java encapsulation. Next will will talk about java abstraction, here »

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

SVD | Singular Value Decomposition | Machine Learning Fundamentals

Singular Value Decomposition or SVD is the general purpose useful tool in Numerical Linear Algebra for data...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a...

You might also likeRELATED
Recommended to you