Encapsulation - Program
package com.Selenium.Tests;
public class Encapsulation {
private String name = "SATISH";
private int i = 10;
private int j = 10;
public static void main(String[] args) {
Encapsulation encs = new Encapsulation();
System.out.println(encs.name);
System.out.println(encs.i);
System.out.println(encs.j);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
}
It is defined as wrapping up of data under a Single unit. It is a
protective shield that prevents the data from being accessed by the code
outside the shield.
In Encapsulation data in a class is hidden from other class, so it
is also known as data-hiding.
How is Encapsulation achieved?
Encapsulation is achieved by declaring all the variables in a
class as private and writing public methods in a class to set and get the
values of variables.
package com.Selenium.Tests;
public class Encapsulation {
private String name = "SATISH";
private int i = 10;
private int j = 10;
public static void main(String[] args) {
Encapsulation encs = new Encapsulation();
System.out.println(encs.name);
System.out.println(encs.i);
System.out.println(encs.j);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
}
Comments
Post a Comment