Shading variables and methods in Java

Basically, I would like to know why the static method cannot be obscured by the instance method (I know why this will lead to ambiguity in certain circumstances), while the static variable can be obscured by the instance variable (it applies only to 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 an int area (instance variable) along with a static int area in the superclass, it will throw an error, but this will not happen if declared in the subclass, even if the static int area is still displayed from the subclass.

What exactly is the difference in behavior between trying to shadow a static method using an instance method and trying to shadow a static variable in an instance variable.

Thanks in advance.

+6
source share
6 answers

In your subclass (BedroomFlat), the compiler will not allow you to declare an instance method with the same name as the static method in the base class, because method overriding only applies to instance methods. A class extension only makes instance methods available to the subclass for redefinition (and not class methods, i.e. static). Moreover, when you try to declare a method with the same signature as the static method, the compiler will throw an error stating that you cannot override the static method, since overriding occurs for the instance method.

But the compiler will not stop you from declaring an instance variable with the same name as the static one from the superclass, since variables are not candidates for redefinition.

+1
source

No one can inherit static methods and fields because they belong to a class.

In your case, you do not override getArea(); from the parent, you are trying to create a method with the same signature - and this leads to a compilation error.

+2
source

The fact is that you don’t hide anything here ...

Your instance variable is available directly to my name and NORMALLY you need to write something like ClassName.staticVar to access the static variable. Java only allows you to omit the class name when referencing a static variable.

This is clear, right?

0
source

In Java, static methods and fields apply only to the class, so they cannot be inherited by objects, otherwise this will lead to a compilation error.

0
source

we know that a child inherits a parent, but we also know that static methods cannot be overridden. So in this case you have two methods in a child with the same method signature. This will create a problem because java does not support two methods of the same signature, although here one method is static and the other is non-stationary. but for a static variable this is not so.

0
source

I suppose this gives some problems with polymorphism (or at least how it is implemented). Let's say you do something like this Apartment x = new BedroomFlat(); . Calling the getArea() method, he does not know whether it is a hidden method or overridden, because if it is hidden (static), he must call the one in the Apartment class, but if it is overridden, he must call an instance.

Of course, these cases should usually be avoided (this gives additional problems).

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

0
source

All Articles