KARTHIC RAGUNATH S B’s Post

🚀 Learning Core Java – Method Hiding & Variable Hiding Today I explored an interesting concept in Java — Method Hiding and Variable Hiding. When a class inherits properties and behavior from another class, we usually talk about method overriding. But things behave differently when static methods and variables are involved. 🔹 Method Hiding (Static Methods) In Java: ✔ Instance methods → can be overridden ✔ Static methods → cannot be overridden If a child class defines a static method with the same signature as the parent: 👉 It does NOT override the method 👉 Instead, it hides the parent method This is called Method Hiding. 🔎 Important: • The method that gets executed depends on the reference type, not the object type • This is resolved at compile-time (not runtime) 🔹 Variable Hiding When a child class declares a variable with the same name as in the parent class: 👉 The child variable hides the parent variable This applies to: ✔ Static variables ✔ Instance variables 🔎 How to Access Parent Members? We use the super keyword to access hidden members of the parent class: ✔ super.variable → Access parent variable ✔ super.method() → Access parent method (if needed) 💡 Key Insight 👉 Instance methods → Overriding (Runtime Polymorphism) 👉 Static methods → Method Hiding (Compile-time behavior) 👉 Variables → Always Hiding (No overriding concept) Understanding this difference helps in avoiding confusion and writing predictable and clean Java code. Excited to keep strengthening my Core Java fundamentals! 🚀 #CoreJava #MethodHiding #VariableHiding #JavaProgramming #ObjectOrientedProgramming #JavaDeveloper #ProgrammingFundamentals #LearningJourney

  • diagram

To view or add a comment, sign in

Explore content categories