What is the difference between method hiding and method overriding in OOP?

Object Oriented Programming Questions



47 Short 36 Medium 25 Long Answer Questions Question Index

What is the difference between method hiding and method overriding in OOP?

Method hiding and method overriding are two different concepts in object-oriented programming (OOP).

Method hiding refers to the situation where a subclass defines a static method with the same name as a static method in its superclass. In this case, the subclass method hides the superclass method, and the subclass method is called when the method is invoked on an object of the subclass. This is determined at compile-time based on the reference type, not the actual object type. Method hiding is achieved by using the "new" keyword in C# or by not using any specific keyword in Java.

On the other hand, method overriding occurs when a subclass defines a method with the same name and signature as a method in its superclass. In this case, the subclass method overrides the superclass method, and the subclass method is called when the method is invoked on an object of the subclass. This is determined at runtime based on the actual object type. Method overriding is achieved by using the "override" keyword in C# or by using the "@Override" annotation in Java.

In summary, the main difference between method hiding and method overriding is that method hiding is determined at compile-time based on the reference type, while method overriding is determined at runtime based on the actual object type.