0

According to the documentation

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

While in case of static methods

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

So, I have tested the code example shown there with addition of more use cases:
The super class Animal:

public class Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Animal");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal");
    }
}

The subclass Cat:

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal animal = new Animal();
        Animal.testClassMethod();
        Cat.testClassMethod();
        animal.testInstanceMethod();
        myAnimal.testInstanceMethod();
    }
}

The output here is:

The static method in Animal
The static method in Cat
The instance method in Animal
The instance method in Cat

So, I still see no actual difference between overriding the superclass instance method with subclass instance method with the same signature and overriding (hiding) the superclass static (class) method with subclass static method with the same signature.
What am I missing here?

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • You are seeing a different as you are calling `myAnimal.testInstanceMethod` which is actually calling the method on `Cat` whilst the static method (if you would do `myAnimal.testClassMethod` would be the one from Animal. – M. Deinum Feb 10 '22 at 09:57
  • 1
    Try `animal = cat; animal. ...`. – Joop Eggen Feb 10 '22 at 09:59

2 Answers2

3

Hiding it means that you can't call super.method() in the sub class's implementation.

So for example

class Cat extends Animal {
    public static void testClassMethod() {
        super.testClassMethod(); //this is not possible
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        super.testInstanceMethod(); //this is fine
        System.out.println("The instance method in Cat");
    }

}
Ivo
  • 18,659
  • 2
  • 23
  • 35
  • 1
    I see, thank you. But I still not sure. When I'm trying to put `super.testClassMethod()` in subclass `Cat` method `testClassMethod()` I get `"Cat.super" cannot be referenced from a static content` notification. But I still can put `super.testClassMethod()` reference inside the `Cat` `testInstanceMethod` and it successfully prints `The static method in Animal` from there! – Prophet Feb 10 '22 at 10:11
  • 1
    So currently I see the issue is not between `hiding` vs `overriding`. You simply `cannot be referenced from a static content` to any upper class object etc. – Prophet Feb 10 '22 at 10:14
  • The "cannot be referenced from a static context" happens because you try to access a non-static atribute or method from a static method. – Raul Lapeira Herrero Feb 10 '22 at 10:44
2

You can call super() in an object method (not static), but you can not call super in a static method because there is no "super" object to call to.