Encapsulation in Java
How to achieve encapsulation in java
Example 01-
Let' see this with the help of a very basic Student example please have a look first on the code then I will clarify you step by step what is happening in the code snippet but first please go through the code example carefully.
// Encapsulation example with very simple logic
class Student {
// Private variable (data hiding)
private String name;
// Method to set name (controlled access)
public void setName(String n) {
name = n; // assigning value to private variable
}
// Method to display name
public void displayName() {
System.out.println("Student name is: " + name);
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create object
Student s1 = new Student();
// Set name using method (not directly)
s1.setName("John");
// Display name using method
s1.displayName();
}
}
Student name is: John
let me clarify you what is actually happening in the above code example we created a class called Student and we created a private variable name for data hiding purpose and also created a public method setName() for assigning value to that variable and one method for displaying the name and in the main method we are creating a object of Student class and setting the name of Student by the method not directly because name is private it is only accessed directly inside Student class only achieving data hiding here and then displaying the name simply hope that makes some sense to you.
Example 02-
// Encapsulation example with simple logic
class Car {
// Private variables (data hiding)
private String carName;
private int carYear;
// Method to set car details (controlled access)
public void setCarDetails(String name, int year) {
carName = name;
carYear = year;
}
// Method to display car details
public void displayCarDetails() {
System.out.println("Car Name: " + carName);
System.out.println("Car Year: " + carYear);
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create object
Car car1 = new Car();
// Set car details using method
car1.setCarDetails("Toyota", 2022);
// Display car details using method
car1.displayCarDetails();
}
}
Car Name: Toyota
Car Year: 2022
Let me explain you so quick what is actually happening in the above code snippet there's a class called car having two private variables carName and carYear for restricting the access and there we created two public methods setCarDetails() and displayCarDetails() so that we can access these from our Main class then we can use this for setting car details using this method only hiding data here without this method we can't directly access those two carName and carYear variable hope you are getting and finally in the main method we created the object in our Main class and setting and displaying car details finally achieving data hiding simply.
Comments
Post a Comment
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.