Scanner class Methods in Java

Java - Encapsulation , Data Hiding and Access Modifiers

Encapsulation and Data Hiding 

In the programs, that we have discussed so far,there was no restriction on  accessing members of a class outside the class or even packages. For example, the fields length and breath of Rectangle class were accessible from any other class outside the class Rectangle.

If every member of any class can be accessible from other classes then it would be impossible to understand,maintain and debug programs.To remove this conflict situation we can use Encapsulation and Data Hiding.

What is Encapsulation?

Encapsulation is a feature of Object Oriented Programming. It binds the data and methods together in a unit.In other words, Encapsulation is a way of wrapping up data and related methods in a single unit called Class.

The only way to access data is through the methods which are wrapped in the class.These methods provide interaction between two or more classes.

Example:

Encapsulation Example
Encapsulation Example

The both classes are related but independent of each other.Student class can still access the Library class for borrowing books or for getting details about books.

Encapsulation is implemented by collecting the data and methods in a unit or class. Encapsulation can help us in accessing data from other classes.We use Access Modifiers for this purpose.

Advantages of Encapsulation:

  1. It protects the data from accidental manipulation by methods outside the class.
  2. Any changes made in data and methods of current class does not effect to another classes.

What is Data Hiding?

It is the mechanism of hiding data of a class from the outside world  (classes) so that any other class either intentionally or unintentionally cannot modify it.Data Hiding can be achieved by making or using private Access modifier.

What are Access Modifiers? How to use Access Modifiers?

Access modifiers are basically keywords which are only used to apply Encapsulation and Data Hiding on data and methods.They provide scope to data members of a class.These are reserved keywords. Access modifiers can make data of a class public or private.

Types of Access Modifiers:

There are four types of Access modifiers in java:
  1. private
  2. default
  3. protected 
  4. public
  • private Access Modifier: This provide a scope to data and methods within the class. These methods and data cannot be accessed outside the class.This is very useful as it provides Data Hiding.The data cannot be modified or misused by other classes.Example, private String firstName;
  • default Access Modifier: When you do not specify any access modifier then java uses its default modifier. This provides a scope where data can be accessed in a particular package.All the class in this package can access these data members .Outside classes cannot access data.ExampleString firstName;
  • protected Access Modifier: This allows data to be accessed within the package and outside through child classes. The child or derived classes can access the data.Example, protected String firstName;
  • public Access Modifier: It provides a global scope.All classes can access public data members and methods.It applies no restrictions on accessing data.Example, public String firstName;

We will implement access modifiers in the upcoming post.Thanks for reading!Keep Learning.



Comments

Post a Comment

If you have any doubt, ask here