Java Encapsulation Basics

🚀 Starting My Java Learning Journey – Day 16 🔹 Topic: Encapsulation in Java. Encapsulation is one of the core OOP concepts. It is the process of wrapping data (variables) and code (methods) into a single unit (class). It also helps in data hiding. 📌 How to Achieve Encapsulation? ✔ Declare variables as private. ✔ Provide public getter and setter methods to access and update values. 📌 Example Program class Student { private String name; // Getter method public String getName() { return name; } // Setter method public void setName(String name) { this.name = name; } } public class Main { public static void main(String[] args) { Student s1 = new Student(); s1.setName("John"); System.out.println(s1.getName()); } } Output: John 💡 Key Points: ✔ Protects data from unauthorized access. ✔ Improves security and flexibility. ✔ Achieved using private variables + getters/setters. #Java#JavaLearning #Programming #BackendDevelopment #CodingJourney #Encapsulation #OOP#

To view or add a comment, sign in

Explore content categories