Java Polymorphism Trick You Didn't Expect! 🤯 | Java Interview Question #shorts #interview #java

preview_player
Показать описание
Confused why this prints 10 instead of 20 in Java? Let's break it down!

That’s because Java only uses polymorphism for methods, not for variables (fields). Even though obj is an instance of Child, it’s declared as a Parent, so Java accesses the value field from the Parent class.
This is called field hiding, not overriding. Java decides which variable to access based on the reference type — not the actual object type.

It's a common trap in coding interviews and real projects!

👍 Follow @CodeSnap-UR for more Java tips, tricks, and interview-ready explanations!
Рекомендации по теме
Комментарии
Автор

🔥 Did this trick you? Comment “YES” if you thought it would print 20! 💬

CodeSnap-UR
visit shbcf.ru