-->

Variable and Method shadowing in Java

2020-07-10 09:02发布

问题:

Basically I would like to know why a static method cannot be shadowed by an instance method, (I know why, it will lead to ambiguity in certain circumstances), whereas a static variable can be shadowed by an instance variable (it applies only for subclasses).

Example:

public class Apartment{

    static int area = 10;

    public static int getArea(){
        return area;
    }
}

class BedroomFlat extends Apartment {

    int area = 10;// no problem at all 

    public int getArea(){ // illegal line it cannot hide the super static method
        return area;
    }
}

So if I tried to declare int area (instance variable) along with the static int area in the super class it would give an error but it does not happen when declared in the subclass even though the static int area is still visible from the subclass.

What's exactly the difference in terms of behavior between trying to shadowing a static method with an instance method and trying to shadowing a static variable with an instance variable.

Thanks in advance.

回答1:

In your sub class (BedroomFlat), compiler will not allow you to declare a instance method with the same name as that of static method in the base class because method overriding is only applicable to instance methods. Extending a class only make instance methods available to the sub class for overriding(and not class methods i.e. static). Moreover, when you try to declare a method with same signature as that of a static method, compiler will throw an error saying you cannot override a static method, as overriding takes place for instance method.

But compiler will not stop you from declaring a instance variable with the same name as static one from super class, because variables are not the candidates for overriding.



回答2:

Nobody could inherit static methods and fields because they are belong to class.

In your case you are not overriding getArea(); from parent, you are attempting to create method with the same signature - and it leads to compilation error.



回答3:

The thing is: You are not shadowing anything here at all...

Your instance variable is accessible directly my naming it and NORMALLY you would have to write something like ClassName.staticVar to access a static variable. Java only allows you to omit the class name when referncing a static variable.

That makes it clear, doesn't it?



回答4:

In Java static methods and fields only belong to class, so they could not inherited by objects, or it would leads to compile error.



回答5:

we know that the child inherits the parent but we also know that the static methods can't be overridden. So in this case You would have two methods in the child with the same method signature. That will create the problem as java does not support two method of same signature, although here one method is static and another one is non-static. but for static variable this is not the case.



回答6:

My guess is that this gives some problems at polymorphism (or at least how it is implemented). Let's say you do something like this Apartment x = new BedroomFlat(); . Calling the method getArea(),it doesn't know if it's a hidden method or overridden,because if it's hidden(static) it should call the one in class Apartment, but if it's overridden it should call the instance one.

Of course,usually these cases should be avoided(it gives unnecesarry headaches).

EDIT: I found this link,where in the bottom of the page,you can see the table of method hiding/overriding between static and instance methods. Idk why it is how it is,but at least we know it is written somewhere (https://docs.oracle.com/javase/tutorial/java/IandI/override.html)