Encapsulation is one of the 4 essential OOP concepts. The alternative three are inheritance, polymorphism, and abstraction.
Encapsulation in Java is a mechanism of wrapping the facts (variables) and code performing at the records (techniques) collectively as a single unit. In encapsulation, the variables of a category will be hidden from different classes, and can be accessed only via the methods of their current class. Therefore, it's also referred to as statistics data hiding.
To gain encapsulation in Java −
Following is an instance that demonstrates a way to achieve Encapsulation in Java –
The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters. Therefore, any class that wants to access the variables should access them through these getters and setters.
The variables of the EncapTest class can be accessed using the following program –
This will produce the following result −
Java Related Interview Questions
|File Maker Interview Questions||J2EE Interview Questions|
|Core Java Interview Questions||Android Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|JavaMail API Interview Questions||Java collections framework Interview Questions|
|Java 8 Interview Questions||Java Collections Interview Questions|
|Java Exception Handling Interview Questions||Java Concurrency Interview Questions|
|Java String Interview Questions||Java Hadoop Developer Interview Questions|
|Java Serialization Interview Questions||Java Programmer Interview Questions|
|Java Inheritance Interview Questions||Java IO Interview Questions|
|Hadoop Testing Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.