Vous êtes sur la page 1sur 3

Data Hiding

It’s way to protect our data from the outside world. It means if I made my instance variable public, then anyone
can change its state. But if we make our instance variable private/protected then actually we are restricting
outside entities from making changes to it.

If a data member is declared "private", then it can only be accessed within the same class. No outside class can
access data member of that class. If we need to access these variables, we have to use public "getter" and
"setter" methods.

Getter and Setter's methods are used to create, modify, delete and view the variables values.

The following code is an example of getter and setter methods:

class Account{
private int account_number;
private int account_balance;
// getter method
public int getBalance() {
return this.account_balance;
}
// setter method
public void setNumber(int num) {
this.account_number = num;
}
}

In above example, getBalance() method is getter method that reads value of variable account_balance and
setNumber() method is setter method that sets or update value for variable account_number.

Abstraction
Abstraction = different implementations of the same interface.

In Java, abstraction is achieved using abstract classes and interfaces.

Abstraction means making things general i.e., instead of creating a very specific class when we create base
classes or interfaces and then implement them to get our specific class.

Example: class Animal { } class Lion implements Animal { }

So here for Lion class we have a generalized class i.e., Animal. This represents abstraction

public interface Animal{


public String getType();
}

class Lion implements Animal {


private String animalType = "WILD";

@Override
public String getType() {
return this.animalType;
}
}
Encapsulation in Java
 Encapsulation is a mechanism of binding code and data together in a single unit.
 All the methods and variables are wrapped together in a single class.

 Set the instance variables private so that these private variables cannot be accessed directly by other classes.
 Set getter and setter methods of the class as public so that we can set and get the values of the fields.

SAMPLE PROGRAM.
File-1: EncapsulationClassOne.java
public class EncapsulationClassOne {

// Variables declared as private


// These private variables can only be accessed by public methods of class

private int age;


private String name;

// getter method to access private variable


public int getAge(){
return age;
}

public String getName(){


return name;
}

// setter method to access private variable


public void setAge(int inputAge){
age = inputAge;
}

public void setName(String inputName){


name = inputName;
}
}
File-2: EncapsulationClassTwo.java

public class EncapsulationClassTwo {

public static void main(String [] args){

EncapsulationClassOne obj = new EncapsulationClassOne();


// Setting values of the variables
obj.setAge(25);
obj.setName("Abdul Ahad");

System.out.println("My name is "+ obj.getName());


System.out.println("My age is "+ obj.getAge());
}
}

In the above example, we can find

 All data member (variables) are declared as private. That means it can only be accessed within the same
class. Other class cannot access these private variables.
 To access these private variables from other classes, we used public getter and setter methods such as
getAge(), getName(), setAge(), setName(). So, the data can be accessed by public methods when we can set
the variables private and hide their implementation from other classes.
 This way we call encapsulation as data hiding.
 Encapsulation = information hiding = data hiding.

To achieve encapsulation in Java −

 Declare the variables of a class as private.


 Provide public setter and getter methods to modify and view the variables values.
Benefits of Encapsulation

 The fields of a class can be made read-only or write-only.


 A class can have total control over what is stored in its fields.

Tightly Encapsulated Class


A class is said to be tightly encapsulated if and only one if each and every variable declared as Private.

Weather class contains corresponding getter or setter Methods or not and this weather this methods are
declared as public or not” This things we are not required to check.

class Account{

private int balance;

public int getBalance()


{
return balance();
}
}
Note: If the parent class is not Tightly Encapsulated then child classes also not Tightly Encapsulated.

Vous aimerez peut-être aussi