天天看點

override and hidingOverriding and Hiding Methods

Overriding and Hiding Methods

Instance Methods

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.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.

When overriding a method, you might want to use the

@Override

annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error. For more information on

@Override

, see

Annotations

.

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.

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.

Consider an example that contains two classes. The first is

Animal

, which contains one instance method and one static method:

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 second class, a subclass of

Animal

, is called

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.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}
      

The

Cat

class overrides the instance method in

Animal

and hides the static method in

Animal

. The

main

method in this class creates an instance of

Cat

and invokes

testClassMethod()

on the class and

testInstanceMethod()

on the instance.

The output from this program is as follows:

The static method in Animal
The instance method in Cat
      

As promised, the version of the hidden static method that gets invoked is the one in the superclass, and the version of the overridden instance method that gets invoked is the one in the subclass.

大體上總結一下吧:

靜态方法隻能繼承不能重寫(Override),對于父類和子類有相同的靜态方法是Java中是隐藏,具體調用哪一個方法根據你引用的類型(即  Animal myAnimal=new Cat();Animal是引用類型)andro

轉載于:https://blog.51cto.com/4747857/1358674