Java Encapsulation


In Java, encapsulation is the process of binds together the data and methods in a single unit. It's safe from outside interference and misuse.


Example: Capsule, it is wrapped with different medicines.

Java Encapsulation

Data encapsulation is the process of binding the data, and the methods that use them and data abstraction is the process of showing only the interfaces and hiding the implementation details from the user.

 

The primary goal of encapsulation is the insulation of a particular object class internal detail and show the interfaces.

 

If the data member is declared Private then it accesses within that package or class only.


Example:

class Book{  
	private String Bookname;  
	//getter method for name  
	public String getName()
    {  
		return Bookname;  
	}  
	//setter method for name  
	public void setName(String Bookname)
    {  
		this.Bookname=Bookname; 
	}  
}  
public class Encapsulation{  
	public static void main(String[] args){  
		Book b=new Book(); 
        //setting the value for name field
		b.setName("Programtpoint");  
		//getting value for name field  
		System.out.println(b.getName());  
	}  
}  



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.