Encapsulation:
--------------
Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes.
encapsulation in java
We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it..
package oopsconcept;
public class Mobile {
private String manufacturer;
private String operating_system;
public String model;
private int cost;
//Constructor to set properties/characteristics of object
Mobile(String man, String o,String m, int c){
this.manufacturer = man;
this.operating_system=o;
this.model=m;
this.cost=c;
}
//Method to get access Model property of Object
public String getModel(){
return this.model;
}
// We can add other method to get access to other properties
}
Advantage of Encapsulation in java:
--------- ------------ -----
By providing only setter or getter method, you can make the class read-only or write-only.
It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only, you can write the logic inside the setter method.
Simple example of encapsulation in java:
--------- ---------- ---------
public class Student{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
}
class Test{
public static void main(String[] args){
Student s=new Student();
s.setname("jagan");
System.out.println(s.getName());
}
}
Output:jagan
--------------
Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes.
encapsulation in java
We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it..
package oopsconcept;
public class Mobile {
private String manufacturer;
private String operating_system;
public String model;
private int cost;
//Constructor to set properties/characteristics of object
Mobile(String man, String o,String m, int c){
this.manufacturer = man;
this.operating_system=o;
this.model=m;
this.cost=c;
}
//Method to get access Model property of Object
public String getModel(){
return this.model;
}
// We can add other method to get access to other properties
}
Advantage of Encapsulation in java:
--------- ------------ -----
By providing only setter or getter method, you can make the class read-only or write-only.
It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only, you can write the logic inside the setter method.
Simple example of encapsulation in java:
--------- ---------- ---------
public class Student{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
}
class Test{
public static void main(String[] args){
Student s=new Student();
s.setname("jagan");
System.out.println(s.getName());
}
}
Output:jagan