Java Access Modifiers Explained

🚀 Day 1/100 – Java Practice Challenge Today I started my #100DaysOfCode journey focusing on core Java concepts. 🔹 Topics Covered: Java Access Modifiers Understanding private, default, protected, public How visibility works across classes 💻 Practice Code: 🔸 1. Private (accessible only within same class) class PrivateExample { private int value = 10; public static void main(String[] args) { PrivateExample obj = new PrivateExample(); System.out.println(obj.value); // ✅ accessible inside same class } } 🔸 2. Default (accessible within same package) class DefaultExample { int value = 20; // default public static void main(String[] args) { DefaultExample obj = new DefaultExample(); System.out.println(obj.value); // ✅ same package } } 🔸 3. Protected (same package + child class) class ProtectedExample { protected int value = 30; } class Child extends ProtectedExample { public static void main(String[] args) { Child obj = new Child(); System.out.println(obj.value); // ✅ inherited access } } 🔸 4. Public (accessible everywhere) class PublicExample { public int value = 40; public static void main(String[] args) { PublicExample obj = new PublicExample(); System.out.println(obj.value); // ✅ accessible everywhere } } 📌 Key Learning: private → accessible only within the same class default → accessible within the same package protected → same package + inheritance public → accessible from anywhere Access modifiers are used to control visibility and secure data in Java. #100DaysOfCode #Java #JavaDeveloper #CodingJourney #LearningInPublic #Programming

To view or add a comment, sign in

Explore content categories