Java OOP Mistake: Methods vs Variables Polymorphism

🚫 Most Common Java OOP Mistake (Even in Interviews) Many developers expect this to print 20: class Shape { int x = 10; void draw() { System.out.println("Shape draw"); } } class Circle extends Shape { int x = 20; void draw() { System.out.println("Circle draw"); } } public class Test { public static void main(String[] args) { Shape s = new Circle(); System.out.println(s.x); // ❌ prints 10 s.draw(); // ✅ prints "Circle draw" } } Why this happens? 👉 Java treats variables and methods differently: Methods → runtime (object decides) Variables → compile time (reference decides) So: s.draw() → uses Circle (object type) s.x → uses Shape (reference type) Golden Rule 👉 “Methods are polymorphic, variables are not.” This tiny concept is one of the most common sources of confusion in OOP—and a favorite interview trap. #Java #OOP #Programming #CodingInterview #SoftwareDevelopment

To view or add a comment, sign in

Explore content categories