Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Implement Method Hiding in Java

Published on Oct 22,2019 35 Views
20 / 22 Blog from Java Collections

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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:

 

What is Method Hiding?

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.

Java Logo

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.

 

Method Hiding Java Code

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:

Method-Hiding-in-Java

 

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.

 

Summary

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.