重写与隐藏 Java-混淆

我对重写与隐藏在 Java 中的区别感到困惑。有人能提供更多关于这些差异的细节吗?我读了 Java 教程,但示例代码仍然让我感到困惑。

更明确地说,我非常理解重写。我的问题是,我看不出隐藏有什么不同,除了一个在实例级别,而另一个在类级别。

查看 Java 教程代码:

public class Animal {
public static void testClassMethod() {
System.out.println("Class" + " method in Animal.");
}
public void testInstanceMethod() {
System.out.println("Instance " + " method in Animal.");
}
}

然后我们有一个子类 Cat:

public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class 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();
}
}

然后他们说:

该方案的产出如下:

动物中的类方法。

Cat 中的实例方法。

对我来说,直接从 Animal类调用类方法 testClassMethod()来执行 Animal类中的方法这一事实是显而易见的,没有什么特别之处。然后,他们从对 myCat的引用中调用 testInstanceMethod(),所以很明显,执行的方法就是 Cat实例中的方法。

在我看来,调用隐藏的行为就像覆盖一样,所以为什么要区分它们呢?如果我使用上面的类运行这段代码:

Cat.testClassMethod();

我去拿: Cat 中的 class 方法。 但是如果我从 Cat 身上移除 testClassMethod(),我会得到: Animal 中的 class 方法。

这表明,在子类中编写一个与父类具有相同签名的静态方法几乎可以完成重写。

希望我能解释清楚我的困惑,也希望有人能给我一些启示。非常感谢!

53406 次浏览

If I understand your question properly then the answer is "you already are overriding".

"Which shows me that writing a static method, with the same name as in the parent, in a subclass pretty much does an override."

If you write a method in a subclass with exactly the same name as a method in a superclass it will override the superclass's method. The @Override annotation is not required to override a method. It does however make your code more readable and forces the compiler to check that you are actually overriding a method (and didn't misspell the subclass method for example).

Overriding basically supports late binding. Therefore, it's decided at run time which method will be called. It is for non-static methods.

Hiding is for all other members (static methods, instance members, static members). It is based on the early binding. More clearly, the method or member to be called or used is decided during compile time.

In your example, the first call, Animal.testClassMethod() is a call to a static method, hence it is pretty sure which method is going to be called.

In the second call, myAnimal.testInstanceMethod(), you call a non-static method. This is what you call run-time polymorphism. It is not decided until run time which method is to be called.

For further clarification, read Overriding Vs Hiding.

This is the difference between overrides and hiding,

  1. If both method in parent class and child class are an instance method, it called overrides.
  2. If both method in parent class and child class are static method, it called hiding.
  3. One method cant be static in parent and as an instance in the child. and visa versa.

enter image description here

Static methods are hidden, non-static methods are overriden. The difference is notable when calls are not qualified "something()" vs "this.something()".

I can't really seem to put it down on words, so here goes an example:

public class Animal {


public static void something() {
System.out.println("animal.something");
}


public void eat() {
System.out.println("animal.eat");
}


public Animal() {
// This will always call Animal.something(), since it can't be overriden, because it is static.
something();
// This will call the eat() defined in overriding classes.
eat();
}


}




public class Dog extends Animal {


public static void something() {
// This method merely hides Animal.something(), making it uncallable, but does not override it, or alter calls to it in any way.
System.out.println("dog.something");
}


public void eat() {
// This method overrides eat(), and will affect calls to eat()
System.out.println("dog.eat");
}


public Dog() {
super();
}


public static void main(String[] args) {
new Dog();
}


}

OUTPUT:

animal.something
dog.eat

Overriding happens only with instance methods. When the type of the reference variable is Animal and the object is Cat then the instance method is called from Cat (this is overriding). For the same acat object the class method of Animal is used.

public static void main(String[] args) {
Animal acat = new Cat();
acat.testInstanceMethod();
acat.testClassMethod();


}

Output is:

The instance method in Cat.
Class method in Animal.
public class First {


public void Overriding(int i) {  /* will be overridden in class Second */ }


public static void Hiding(int i) {  /* will be hidden in class Second
because it's static */ }
}




public class Second extends First {


public void Overriding(int i) {  /* overridden here */  }


public static void Hiding(int i) {  /* hides method in class First
because it's static */ }
}

The rule for memorizing is simple: a method in an extending class can't change static to void and can't change void to static. It will cause of compile-error.

But if void Name is changed to void Name it's Overriding.

And if static Name is changed to static Name it's Hiding. (Both the static method of the subclass as well as the one of the superclass can be called, depending on the type of the reference used to call the method.)

How is static method hiding happening in java? Cat class is extending Animal class. So in Cat class will have both static methods (i mean Child class's static method and Parent class's static method) But how JVM hiding Parent static method? How it's dealing in Heap and Stack?

Based on my recent Java studies

  • method overriding, when the subclass have the same method with the same signature in the subclass.
  • Method hiding, when the subclass have the same method name, but different parameter. In this case, you're not overriding the parent method, but hiding it.

Example from OCP Java 7 book, page 70-71:

public class Point {
private int xPos, yPos;
public Point(int x, int y) {
xPos = x;
yPos = y;
}


public boolean equals(Point other){
.... sexy code here ......
}


public static void main(String []args) {
Point p1 = new Point(10, 20);
Point p2 = new Point(50, 100);
Point p3 = new Point(10, 20);
System.out.println("p1 equals p2 is " + p1.equals(p2));
System.out.println("p1 equals p3 is " + p1.equals(p3));
//point's class equals method get invoked
}
}

but if we write the following main:

  public static void main(String []args) {
Object p1 = new Point(10, 20);
Object p2 = new Point(50, 100);
Object p3 = new Point(10, 20);
System.out.println("p1 equals p2 is " + p1.equals(p2));
System.out.println("p1 equals p3 is " + p1.equals(p3));
//Object's class equals method get invoked
}

In the second main, we using the Object class as static type, so when we calling the equal method in Point object, it's waiting a Point class to arrive as a parameter,but Object coming. So the Object class equals method getting run, because we have an equals(Object o) there. In this case, the Point's class equals dosen't overrides, but hides the Object class equals method.

In this code snippet I use 'private' access modifier instead of 'static' to show you difference between hiding methods and overriding methods.

class Animal {
// Use 'static' or 'private' access modifiers to see how method hiding work.
private void testInstancePrivateMethod(String source) {
System.out.println("\tAnimal: instance Private method calling from "+source);
}
public void testInstanceMethodUsingPrivateMethodInside() {
System.out.println("\tAnimal: instance Public method with using of Private method.");
testInstancePrivateMethod( Animal.class.getSimpleName() );
}


// Use default, 'protected' or 'public' access modifiers to see  how method overriding work.
protected void testInstanceProtectedMethod(String source) {
System.out.println("\tAnimal: instance Protected method calling from "+source);
}
public void testInstanceMethodUsingProtectedMethodInside() {
System.out.println("\tAnimal: instance Public method with using of Protected method.");
testInstanceProtectedMethod( Animal.class.getSimpleName() );
}
}




public class Cat extends Animal {
private void testInstancePrivateMethod(String source) {
System.out.println("Cat: instance Private method calling from " + source );
}
public void testInstanceMethodUsingPrivateMethodInside() {
System.out.println("Cat: instance Public method with using of Private method.");
testInstancePrivateMethod( Cat.class.getSimpleName());
System.out.println("Cat: and calling parent after:");
super.testInstanceMethodUsingPrivateMethodInside();
}


protected void testInstanceProtectedMethod(String source) {
System.out.println("Cat: instance Protected method calling from "+ source );
}
public void testInstanceMethodUsingProtectedMethodInside() {
System.out.println("Cat: instance Public method with using of Protected method.");
testInstanceProtectedMethod(Cat.class.getSimpleName());
System.out.println("Cat: and calling parent after:");
super.testInstanceMethodUsingProtectedMethodInside();
}


public static void main(String[] args) {
Cat myCat = new Cat();
System.out.println("----- Method hiding -------");
myCat.testInstanceMethodUsingPrivateMethodInside();
System.out.println("\n----- Method overriding -------");
myCat.testInstanceMethodUsingProtectedMethodInside();
}
}

Output:

----- Method hiding -------
Cat: instance Public method with using of Private method.
Cat: instance Private method calling from Cat
Cat: and calling parent after:
Animal: instance Public method with using of Private method.
Animal: instance Private method calling from Animal


----- Method overriding -------
Cat: instance Public method with using of Protected method.
Cat: instance Protected method calling from Cat
Cat: and calling parent after:
Animal: instance Public method with using of Protected method.
Cat: instance Protected method calling from Animal
public class Parent {


public static void show(){
System.out.println("Parent");
}
}


public class Child extends Parent{


public static void show(){
System.out.println("Child");
}
}


public class Main {


public static void main(String[] args) {
Parent parent=new Child();
parent.show(); // it will call parent show method
}
}


// We can call static method by reference ( as shown above) or by using class name (Parent.show())

The linked java tutorial page explains the concept of overriding and hiding

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.

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:

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

Coming back to your example:

Animal myAnimal = myCat;


/* invokes static method on Animal, expected. */
Animal.testClassMethod();


/* invokes child class instance method (non-static - it's overriding) */
myAnimal.testInstanceMethod();

Above statement does not show hiding yet.

Now change the code as below to get different output:

  Animal myAnimal = myCat;
  

/* Even though myAnimal is Cat, Animal class method is invoked instead of Cat method*/
myAnimal.testClassMethod();
  

/* invokes child class instance method (non-static - it's overriding) */
myAnimal.testInstanceMethod();

In addition to the examples listed above, here is a small sample code to clarify the distinction between hiding and overriding:

public class Parent {


// to be hidden (static)
public static String toBeHidden() {
return "Parent";
}


// to be overridden (non-static)
public String toBeOverridden() {
return "Parent";
}


public void printParent() {
System.out.println("to be hidden: " + toBeHidden());
System.out.println("to be overridden: " + toBeOverridden());
}
}


public class Child extends Parent {


public static String toBeHidden() {
return "Child";
}


public String toBeOverridden() {
return "Child";
}


public void printChild() {
System.out.println("to be hidden: " + toBeHidden());
System.out.println("to be overridden: " + toBeOverridden());
}
}


public class Main {


public static void main(String[] args) {
Child child = new Child();
child.printParent();
child.printChild();
}
}

The call of child.printParent() outputs:
to be hidden: Parent
to be overridden: Child

The call of child.printChild() outputs:
to be hidden: Child
to be overridden: Child

As wee can see from the outputs above (especially the bold marked outputs), method hiding behaves differently from overriding.

Java allows both hiding and overriding only for methods. The same rule does not apply to variables. Overriding variables is not permitted, so variables can only be hidden (no difference between static or non-static variable). The example below shows how the method getName() is overriden and the variable name is hidden:

public class Main {


public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name); // prints Parent (since hiding)
System.out.println(p.getName()); // prints Child (since overriding)
}
}


class Parent {
String name = "Parent";


String getName() {
return name;
}
}


class Child extends Parent {
String name = "Child";


String getName() {
return name;
}
}

At runtime the child version of an overridden method is always executed for an instance regardless of whether the method call is defi ned in a parent or child class method. In this manner, the parent method is never used unless an explicit call to the parent method is referenced, using the syntax ParentClassName.method(). Alternatively, at runtime the parent version of a hidden method is always executed if the call to the method is defined in the parent class.

In method overriding, method resolution is done by the JVM on the basis of runtime object. Whereas in method hiding, method resolution is done by the compiler on the basis of reference. Thus,

If the code would have been written as,

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

The Output would be as below:
Class method in Animal.

It is called hiding because the compiler hides the super class method implementation, when subclass has the same static method.

Compiler has no restricted visibility for overridden methods and it’s only during runtime that it’s decided which one is used.

This is the difference between overriding and hiding:

Animal a = new Cat();

a.testClassMethod() will call the method in parent class since it is an example of method hiding. The method to be called is determined by the type of the reference variable and decided at compile time.

a.testInstanceMethod() will call the method in child class since it is an example of method overriding. The method to be called is determined by the object which is used to call the method at runtime.

I think this is not yet fully explained. Please see the following example.

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");
}
}




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) {
Animal myCat = new Cat();
Cat myCat2 = new Cat();
myCat.testClassMethod();
myCat2.testClassMethod();
        

        

myCat.testInstanceMethod();
myCat2.testInstanceMethod();
}
}

The output will be as follows.

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