Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
In java, you need to be careful about the possibility of method hiding. A method created with the same type and signature in the sub-class can hide variables in a superclass. In this article, we will understand Method Hiding in Java in the following manner:
Method hiding is functionally very similar to methods overriding. In overriding if you create a method in sub-class with the same type and signature in sub-class then it allows calling of methods based on the type of instance.
In the case of static methods with the same type and signature in superclass and sub-class then, then the method in the subclass hides the method in the superclass.
package com.test; class Parent { public static void foo() { System.out.println("Inside foo method in parent class"); } public void bar() { System.out.println("Inside bar method in parent class"); } } class Child extends Parent { // Hiding public static void foo() { System.out.println("Inside foo method in child class"); } // Overriding public void bar() { System.out.println("Inside bar method in child class"); } } public class Code { public static void main(String[] args) { Parent p = new Parent(); Parent c = new Child(); System.out.println("****************Method Hiding*******************"); p.foo(); // This will call method in parent class c.foo(); // This will call method in parent class System.out.println("****************Method overriding*******************"); p.bar(); // This will call method in parent class c.bar(); // This will call method in child class } }
Output:
In the above example, the sub-class Child has static method foo () having the same name and signature as a static method in super-class Parent. When we call p.foo() and c.foo() it calls foo () method in parent class
unlike in method overriding where p.bar() is calling the method in the parent class and c.bar() calls the method in child class.
As static methods are resolved at compile time while compilation first parent class is complied and then child class, and we cannot have two static methods with same name both the foo methods are resolved as foo () method of the parent class.
If a subclass has a static method with the same name and signature as a static method in the superclass, then the method in the super-class will be call irrespective of the fact whether it is called from child class or parent class.
In case of method overriding we override method from parent class, i.e. if a subclass has a non-static method with the same name and signature as a non-static method in the superclass then respective methods are called depending upon reference used, i.e. if object of parent class is used to call non-static method in parent class then method from parent class is used and if object of child class is used to call non-static method in child class then method from child class is used.
With this, we come to the end of this Method Hiding in Java article. Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “Method Hiding in Java” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 19th October,2024 19th October SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co