Question

I prepare for SCJP and I have a bit confusing with this terms from jls.

about overloading from jls:

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

about hiding:

If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible to code in C.

looks like hiding is part of overloading for static methods only.

But these definitions are quite ornate. please clarify difference.

Can you provide concrete examples which allow to feel the difference?

Was it helpful?

Solution

Overloading is when you have 2 or more methods with the same name, but with different parameters. http://en.wikipedia.org/wiki/Method_overloading

Overriding is when the methods are not static and the names and parameters are the same.

For example,

public class A {
   public void test1(String a) {System.out.println(a);}
}

public class B extends A {
   public void test1(String a) {System.out.println(a);}
}

Then you are overriding the method test1.

Hiding only happens if both methods are static.

The distinction between hiding a static method and overriding an instance method has important implications:

The version of the overridden instance method that gets invoked is the one in the subclass. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

From http://docs.oracle.com/javase/tutorial/java/IandI/override.html

OTHER TIPS

Hiding is similar to Overriding (Not overloading, the distinction is very important), but with static methods. Take a look at the following code;

public class A {
    public static void do(){}
}
public class B extends A{
    public static void do(){}
}

Here, B is said to be hiding the inherited do() method rather than overriding it. If you take out the word static from both the method declarations of do() then this is an override instead. Which hidden method is called, depends entirely on what reference type is holding your object, because as we know that method belongs to the class, not the object created by the class. So technically it is legal to do this;

A a = new B();
a.do();

But (despite a holding a B object) because that is a static method the above is the same as just doing this;

A.do();

Overloading, on the other hand is when you change the arguments to the method and create another legal method entirely that shares the same name. For example.

public class A {
    public static void do(){}
}
public class B extends A{
    public static void do(String unicorn){}
}

Now, class B is overloading the do method instead of hiding it. Think of an overloaded method as just an entirely new method for all practical purposes, because that's what it is. Java will treat it as an entirely new method so long as the arguments passed to the method are different.

I hope this helps.

Overloading is creating new methods with the same name but different signature.
Hiding is creating static methods with the same name and signature in different classes. It's much like overriding, but maintaining access to both class level methods.
Hiding is thus in no way part of overloading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top