Pages

Thursday, May 7, 2020

Encapsulation in Java


In this topic will cover all as below:

1. What is Encapsulation?
2. How to achieve of Encapsulation?
3. What are packages and access Modifiers?
4. Advantages of Encapsulation.

1. What is Encapsulation?


Binding of data into single entity is called as Encapsulation.

Encapsulation overview
Fig: Encapsulation Overview



Example: From above image we can say that, all drugs are encapsulated in capsule.

In figure, we can see that variables can't see as its private and methods are visible as its public. So binding of these variables and methods into class (entity) is called as Encapsulation.




2. What are packages and access Modifiers?

To understand Encapsulation, we need to understand below 2 things:
I) Packages
II) Access modifiers

I)  Packages

Packages are nothing but folder where all project classes, files etc has been saved. Below figure we can see that there are 2 folders akash and Rahul where files/code has been saved in their respective folder.
Can be defined in class by package keyword.

Package structure in Java Project
Packages in Encapsulation Project


Advantages of packages:
It improves modularity and readability of the application.


II) Access modifiers:

There are 4 types of Java access modifiers:
1) Private: The access level is only within the class. It cannot be accessed from outside the class.

2) Default: The access level is only within the package. It cannot be accessed from outside the package. If you do NOT write any access level or not given any modifier to class/method etc then that modifier act as default(invisible/not exist in reality).

3) Protected: The access level is within the package and outside the package through child class (means there should be inheritance in classes).
i.e. Package com1 contains class  A and package com2 contains class B. Then the variable and methods can be accessible if and only if there is relation/inheritance between class A and class B.

4) Public: It can be accessible from everywhere. Can be accessible from inside/outside the class, within or outside the package. Globally accessible.

Access Levels are as below : (Scope of modifiers)

Modifiers
Class
Package
Subclass
Global
Public
Y
Y
Y
Y
Protected
Y
Y
Y
N
Default/ No modifier
Y
Y
N
N
Private
Y
N
N
N


2. How to achieve of Encapsulation?

Best example of encapsulation is POJO class (Plain Old Java Object) i.e. Declare variables as private and provide getter-setter methods for use (getter methods for use/getting any data and setter methods to set data)

Program:

package college;

public class Student {

     private int roll_no;
     private String name;

     public int getRoll_no() {
          return roll_no;
     }

     public void setRoll_no(int roll_no) {
          this.roll_no = roll_no;
     }

     public String getName() {
          return name;
     }

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


package college;

public class DisplayStudent {

     public static void main(String[] args) {

          Student s = new Student();
         
          //Set data: using setter methods
          s.setName("Tony Stark");
          s.setRoll_no(3000);
               
          //Get Data: using getter methods
          System.out.println("Student details :-");
          System.out.println("Name : "+s.getName());
          System.out.println("Roll No.: "+s.getRoll_no());
     }
}

/*
Output:
Student details :-
Name : Tony Stark
Roll No.: 3000
*/


3. Advantages of Encapsulation:


1. We can achieve security.

2. Enhancement become very easy.

3. Improve maintenance of application.

4. provide flexibility.

5. If we have Not define the setter method in the class then the fields can be made “Read-only” (i.e. we can only get/read data from getter methods).

6. If we have Not define the getter method in the class then the fields can be made “Write-only” (i.e. we can only write/ set data from setter methods).

4. Disadvantages:

Code length may increase.



NOTE: 
All interview questions & maximum programs on this topic will be covered in another section. 



No comments:

Post a Comment

**** Please let me know if you have any doubts. ****