Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between override and hidden in java?

Tags:

java

I searched a lot. The difference between them is that override is for the instance method and hidden is for the static method. And the hidden is in fact the redefinition of the method. But I still don't get it.If redefinition means that the static method of parent still exists in the subclass, it is just we can't see it? Or why we call it hidden but not any other words? But if it exists, I can't find a way to call the method again. To be honest from a function level I can't find why they are different. Can some one explain it from a deeper level such as memory?

like image 285
hidemyname Avatar asked Feb 28 '26 19:02

hidemyname


1 Answers

Static members(methods and variables) will not be present in the sub class(Child class) object which inherit them but they'll be present as a single copy in the memory.

Static members can be accessed by the class name of both Super class and sub class but they are not physically present in the object of these classes.

Where as when you inherit non-static members, Sub class object in memory will contain both inherited methods as well as the methods of its own. So when you try to write a similar method here, super class method will be overridden. On the other hand as static methods does not participate in inheritance, any similar method you write that is present in super class, new method will run every-time it is asked for. Parent class method is just hidden but not overridden!

like image 115
Balu Avatar answered Mar 02 '26 13:03

Balu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!