1.What is Encapsulation in Java?
* Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit. In encapsulation, a class's variables are hidden from other classes and can only be accessed by the methods of the class in which they are found
- Java encapsulation is an object-oriented procedure for combining the class's data members and data methods inside the user-defined class. It is important to declare this class private
- Declaring instance variables as private: This restricts direct access to the variables from outside the class, ensuring data hiding.
Syntax:
public class MyClass {
private String dataVariable; // Private instance variable
private int anotherData; // Another private instance variable
}
Referred Links
1.https://www.simplilearn.com/tutorials/java-tutorial/java-encapsulation
Top comments (0)