Java polymorphism, no problem

If there are three classes A. B and C. class B extension a, class C extension B

Class A has equal method:

public boolean equals(A other)
{...}

Level B has equal methods:

public boolean equals(B other)
{...}

And class C have euals methods:

public boolean equals(Object other)
{...}

There are mainly these lines of code:

A a = new A();
C c = new C();
a=c;
System.out.println(a.equals(c));

I can't understand why the class A equals method is being executed

I know that overloaded methods are bound statically But point to "part C of the object" after the alias and the method is equal to class C Why not execute the equals method of class C?

Solution

Methods in subclasses override methods in superclasses only if parameters have the same type

The object class defines an equals () method:

class Object {
    public boolean equals(Object obj) {...}
}

When class A is defined, it inherits the equals routine from object You have defined a new equals, but the parameter types are different, so it will not overwrite the one in the object; Instead, it becomes an overload The result is that a has two overloaded equals methods:

class A {
    public boolean equals(Object obj) {...}  // inherited
    public boolean equals(A other) {...}     // the one you wrote
}

Similarly, equals in B does not override equals, so the result is that the equals method is overloaded three times:

class B {
    public boolean equals(Object obj) {...}  // inherited from Object
    public boolean equals(A other) {...}     // inherited from A
    public boolean equals(B other) {...}     // doesn't override anything
}

In class C, the new equals method will override the one in the object, so there are still three equals methods:

class C {
    public boolean equals(Object other) {...}  // overrides the one in Object
    public boolean equals(A other) {...}       // inherited from A
    public boolean equals(B other) {...}       // inherited from B
}

Now, here's your code:

A a = new A();
C c = new C();
a=c;
System.out.println(a.equals(c));

When you say A. equals (c), the compiler will see that a has a type A. therefore, it will look at the methods in a to see the methods to be executed (the compiler doesn't know that a will have type C at run time; therefore, it won't look at the methods in C.)

There are two options, as shown above:

public boolean equals(Object obj) {...}  // inherited
    public boolean equals(A other) {...}     // the one you wrote

They can all be used on parameter C, because C is an object and it is a. in this case, when one parameter is a subclass of another parameter, the compiler will essentially select the "closest" C is only two subclasses away from a, and it is three subclasses away from object. Therefore, it selects the overload with parameter a, which you define in a Note that this equals method has never been overridden So it executes the code you write in class A

But suppose you write:

System.out.println(a.equals((Object)c));

By converting C to object, you force the compiler to treat it as object Now, when it chooses between overloads, it must choose the one with the object parameter, because the object cannot be automatically converted to a (because not every object is a) Therefore, it chooses the inherited method Moreover, because at runtime, the object is actually class C, and because class C has an equals method equal to object, in this case, it will execute the code written in C language

Your code is a good example of how overloading and overriding work However, in real life, it is a bad idea to write an equals () method. The parameter of this method is not object because it will not be overwritten and may cause confusion In addition, it's a good practice to put @ override on any method you think will override the methods in the superclass In this way, if you use the wrong parameters, the compiler will catch it before you encounter a runtime error that is difficult to trace

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>