Ad

Encapsulation

Encapsulation

What is encapsulation:
  Encapsulation is one of the four basic OOP ideas. The other three are legacy, polymorphism, and reflection. 

Encapsulation in Java is a component of wrapping the information (factors) and code following up on the information (techniques) all together unit. In encapsulation, the factors of a class will be escaped different classes, and can be gotten to just through the strategies for their present class. Along these lines, it is otherwise called information covering up. 

To accomplish Encapsulation in Java − 

Proclaim the factors of a class as private. 

Give open setter and getter techniques to alter and see the factors esteems.


Example:

public class Encapsulation
{
private String empName;
private int empAge;
private int empSsn;
public void setempName(String name)
{
empName = name;
}
public String getempName()
{
return empName;
}
public void setempAge(int age)
{
empAge = age;
}
public int getempAge()
{
return empAge;
}
public void setempSsn(int Ssn)
{
empSsn = Ssn;
}
public int getempSsn()
{
return empSsn;
}

public static void main(String args[])
{
Encapsulation ob1 = new Encapsulation();
ob1.setempName("Usman Hashmi");
ob1.setempAge(19);
ob1.setempSsn(1234);

System.out.println("Name of employer:"  +ob1.getempName());
System.out.println("Age of employer:"  +ob1.getempAge());
System.out.println("SSN of employer:"  +ob1.getempSsn());
}
}

Post a Comment

0 Comments