Problem with java polymorphism alias

If there are 3 classes. A, B, and C. class B extends A, and class C extends B.

class A has an equals method:

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

class B has a method equal to:

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

and class C has an euals method:

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

And the main ones have these lines of code:

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

I cannot understand why the class A equals method is executed.

I know that overloaded methods involve using static binding. But a points to the "C-part of the object" after smoothing and there the method is equal to class C. Why is it not the equals method of class C that will be executed?

+6
source share
3 answers

A method in a subclass overrides a method in a superclass only if the parameters are of the same type.

Object equals():

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

A, equals Object. equals, , Object; . A equals:

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

, equals in B equals, equals:

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
}

C equals Object, equals:

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
}

:

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

a.equals(c), , A A. A, , . ( , A C , C.)

, :

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

C, C Object, A. , , "". C A, Object, A, , A. , equals . , , A.

, :

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

C Object, Object. , , Object, Object A ( Object A). , . C, C equals, Object, , C.

- , . , , equals(), - , Object, , . , @Override , , , . , goof , , , .

+7

, ​​ , . .


1

class A {
    public boolean equals(A Other) {
        System.out.println("A method");
        return true;
    }
}

class C extends A {
    public boolean equals(Object Other) {
        System.out.println("C method");
        return true;
    }
}

public class Driver {
    public static void main(String[] args) {
        A a = new C();
        a.equals(null);
    }
}

1

A method

2

class A {
    public boolean equals(A Other) {
        System.out.println("A method");
        return true;
    }
}

class C extends A {
    public boolean equals(A Other) {
        System.out.println("C method");
        return true;
    }
}

public class Driver {
    public static void main(String[] args) {
        A a = new C();
        a.equals(null);
    }
}

2

C method

# 1 , equals() , . Driver .

# 2 , , , , .

+4

java . ( ), equals. equals A.

@Override
public boolean equals(Object o){ ...}

, (runtime) equals C.

+1

Source: https://habr.com/ru/post/1016023/


All Articles