Method Hiding in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Java allows variables in a superclass to be hidden by methods in subclasses with the same type and signature. A programmer may conceal a method while attempting to override it since method hiding and method overriding are closely linked concepts.

By using overrides, you can write code that executes differently based on the state of an object at runtime. The function may be hidden from a superclass by a static method with the same name in a subclass because a static method in Java cannot be overridden.

What is Method Hiding?

  • When a child class defines a static method with the same signature as a parent class's static method, the child's method hides the parent class's method. Method overriding refers to the same behavior involving instance methods.
  • Method hiding is similar to method overriding in terms of functionality. Overriding allows you to call methods based on the type of instance if you construct a method in a subclass with the same type and signature.
  • When static methods in a superclass and a subclass have the same type and signature, the method in the subclass hides the method in the superclass.
  • Static methods cannot be overridden because method overriding is predicated on dynamic binding at runtime, whereas static methods are bonded via static binding at compile time. As a result, we cannot override static methods.
  • On the other hand, a parent class and its subclasses can have static methods with identical signatures. The static method in the subclass is said to hide the identical method in the parent class in that situation.

Example of Method Hiding in Java

Output:

Example of Method Hiding in java

In the above example, the static method foo() in the sub-class Child has the same name and signature as a static method in the superclass Parent. When we call p.foo() and c.foo(), we call the foo() method in the Parent class.

Unlike in method overriding, where p.bar() calls the Parent class method, and c.bar() calls the Child class method.

Note: The superclass and the subclass must have identical method names, return types, and parameter lists. We cannot override a method that has been designated as final and static.

Features of Method Hiding in Java

  1. Method hiding is as same as compile-time polymorphism because method resolution is handled by the compiler depending on the reference type.
  2. It is often referred to as static polymorphism or early binding.
  3. In method hiding, the Java compiler always resolves method calls depending on the reference type. In Java, runtime polymorphism plays no role in method hiding.
  4. The use of static in method declarations must be consistent across superclasses and subclasses.

Method Hiding Factors (MHF)

  • The method hiding factors are used to assess the invisibility of methods in classes. The percentage of total classes from which a method is invisible is referred to as its invisibility.
  • If another class or object may access an attribute, it is said to be visible. Within a class, an attribute should be hidden. They can be made private to prevent other objects from accessing them.
  • It is determined by adding the invisibility of each method to the other classes in the project. In calculating MHF we take , private = 1, public = 0, and protected=Inheritance tree size / Number of classes. Thus, MHF is a fraction.
  • The number of visible methods reflects the class functionality. MHF will be reduced by increasing overall functionality.
    • A low MHF suggests that the implementation was not properly abstracted. A large percentage of methods are unprotected. Therefore, the risk of error is high.
    • A high MHF suggests a lack of functionality. It could also imply that the design contains a significant proportion of specialized methods that are not reusable. An acceptable MHF range of 8% to 25% has been proposed. It is entirely up to you to make your decision.
  • The method concealment factor should ideally have a high value.
  • The following formula can be used to compute the MHF:

Method Hiding Factors

Example

Output : example of Method Hiding Factors

In this example, we assume that p.sleep() will call the sleep() method from the Parent class and c.sleep() will call the sleep() method from the Child class, just as in overriding, but because sleep() is a static function, we have hidden the sleep() method rather than overriding it.

Since, sleep() is a static function method hiding will be done, because of method hiding the function inside the Child class hides the Parent function and the function c.sleep() gets invoked when p.sleep() is called.

Because static methods are resolved at compile-time, both p.sleep() and c.sleep() are resolved to the sleep() method of the Parent class because the p and c variables are of the Parent type. When the static keyword is removed from both methods, the behavior is as expected.

A static method cannot be overridden as an instance method in Java and vice versa. If you remove the static keyword from a method in a subclass or superclass, you will get a compile-time error that says, This instance method cannot override the static method from Parent or This static method cannot hide the instance method from Parent.

Method Hiding Vs. Method Overriding

Method HidingMethod Overriding
Both methods are required to be static.Both methods are required to be non-static.
The compiler handles method resolution based on the reference type.The JVM handles method resolution based on runtime objects.
It is characterized as compile-time polymorphism, static polymorphism, or early binding.It is characterized as runtime polymorphism, dynamic polymorphism, or late binding.

Example of Method Overriding in java:

In this example, both the superclass and the subclass have methods with the same signature (method name and parameters), and when we try to invoke this method from the subclass, the sub-class method overrides the method in the superclass and is executed.

When the superclass and the subclass have the same instance methods, including parameters, the superclass method is overridden by the method of the subclass when called.

Example of Method Overriding in java

Example of Method Hiding in Java:

When the superclass and the subclass include the same methods, including parameters, and if they are static, and when the superclass method is called, the method of the subclass hides the superclass method.

Example of Method Hiding in Java

Conclusion

  1. Method hiding - "If a subclass (child class) defines a static method with the same signature as a static method in the superclass (parent class), the subclass method conceals the one in the superclass".
  2. Method hiding occurs because static methods are resolved at compile time.
  3. In the method hiding concept, the method call is determined by the reference type. It is also referred to as compile-time polymorphism.
  4. Method overriding occurs in Java when a subclass (child class) contains the same method as the parent class.