Encapsulation In Java

0
39
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Different Types Of Encapsulation In Java
Different Types Of Encapsulation In Java

What is Encapsulation in Java?

As per wiki definition of Encapsulation here .In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

A language mechanism for restricting access to some of the object’s components. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.

Encapsulation or data handling

A good designed always hides modules, internal data, methods and other implementations by clearly separating its APIs from its implementation. Modules talk to each other by the public APIs and oblivious to each others inner design and working principle.
This is known as information hiding or encapsulation.

Encapsulation is important as it decouples the modules and allows them to be developed, tested, optimized, used understood and modified individually hence efficiently without depending on other modulus modules. This ease the maintenance of a large system without fearing that it may harm the other unknown modules ( regression / new code addition becomes easy. Information hiding helps in performance tuning segregating bad modules from good ones.

In a lighter note: Encapsulation is summation of the below written conclusion:

1. Grouping functions and corresponding data into a single class.

  •  Every java class is an example of encapsulation, which can be said to be encapsulated component
  • Every package is an example of encapsulation mechanism.

2. Every class follows encapsulation if it follows the below written principal

  • Encapsulation=Data hiding+Abstraction
  • It must hide data behind methods

Let us discuss one by one:

Datahiding:

I am going to tell you the process and guideline how to make better encapsulation in Java..
Rule..

  • Make your instance variable always private.
  • Always create a setter and getter method that is of public.So if you have n number of instance variables you will create 2*n number of methods.
  • So any object trying to access the variables has to route through setter and getter method.
  • Setter and getter methods will give you more security over normal deceleration.
public class settergetter {
/**
* @param args
*/
private String pencolor;
private int pensize;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public String getPencolor() {
return pencolor;
}
public void setPencolor(String pencolor) {
this.pencolor = pencolor;
}
public int getPensize() {
return pensize;
}
public void setPensize(int pensize) {
this.pensize = pensize;
}
}

As the pensize and pencolor variables are private in nature so it can not be directly accessed by outside this class but the setter and getter methods are public in nature which can be accessed by outside of the class.

so you can modify the setter method as per your requirements…like if you want restrict users to set pensize or pencolor to a particular value you can modify the setter method accordingly with a simple if else logic.

Let us discuss second point abstraction:

Abstraction:

It enables us to highlight only few services may be via methods but hides actual implementation of it.There are two ways by which we can offer abstration:

  • Interfaces
  • Abstract class

In our previous example ..only setter and getter methods are exposed to outside world. But how they work is beyond the reach of the user. So if you change the implementation of setPensize method the outside user will not be able to perform any activity rather they have to abide by the rule you have set.

public void setPensize(int pensize) {
if (pensize>5)
  this.pensize = pensize;
else
this.pensize = 5;
}

In this example the setter method is modified in such a way if the service consumer calls this method to set Pen size, it will not be able to set size below 5.So we are providing more security.

Second point could be , it my requirements are going to change in some other manner, the consumer will not be impacted rather the implementation will be changed internally.

Advantages of Abstraction:(Eventually it becomes advantages of Encapsulation)

  • Provide Security
  • Enhancement becomes very easy.
  • Improves maintainability and more modularity

Disadvantages of Abstraction:(Eventually it becomes disadvantages of Encapsulation)

  • The code becomes lengthy
  • Slows down the execution

Different Types Of Encapsulation In Java

In this post I will describe the different types of Encapsulation.

There are two types of Encapsulation present in java..

  • Tightly Encapsulated Class
  • Loosely encapsulated class(Normal class)

Tightly Encapsulated Class

If a class is having every variable declared as private then it is called tightly encapsulated class.

class test
{
private int age;
private double salary;
private int phoneNumber;
}

Loosely Encapsulated Class:

if a class is having atleast one normal variable or any variable other than private,then it is called Loosely Encapsulated Class.

class test
{
int age;
private double salary;
private int phoneNumber;
}

lets see how it works in parent child relationship:

Encapsulation mode Code
Tightly Encapsulated
class test
{
Private int age;
private double salary;
private int phoneNumber;
}
Loosely Encapsulated
class abc extends test
{
String Address;
}
Tightly Encapsulated
class xyz extends test
{
private String Name;
}

In this above example the parent class test is having 3 private variable, hence the parent class is tightly Encapsulated class.
The class abc is extending test class hence it is having total 4 variables available with it in which one is normal variable(other than private)

Private int age;
private double salary;
private int phoneNumber;
String Address;

As the string is normal variable so it does not pass the condition to be Tightly Encapsulation rule
With the same logic the xyz class when extending to test  is a tightly Encapsulated class.

Encapsulation mode Code
Loosely Encapsulated
class test
{
String Name;
}
Loosely Encapsulated
class xyz extends test
{
private String Name;
}
Loosely Encapsulated
class abc extends xyz
{
private String Name;
}

In the above example test is the parent class which is loosely encapsulated as Name variable is not declared  as private. so class xyz when extends to test , automatically becomes loosely Encapsulated class even though it is having one private variable. Now when abc extends to xyz which is already a loosely Encapsulated hence it becomes loosely encapsulated.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here