简体   繁体   中英

Java Interfaces and Inheritance and Polymorphism

To conceptually understand inheritance, interfaces & polymorphism, I created a couple of classes to test things. The result I am getting is not the one I expected. The code is as follows:

public class Animal implements Comparable{
//Animal constructor & methods
public int compareTo(Object arg0){System.out.println("Animal.compareTo");}
}

public class Bear extends Animal{
//Bear constructor & methods
}

public class PolarBear extends Bear implements Comparable{
//PolarBear constructor & methods
public int compareTo(Object arg0) {System.out.println("PolarBear.compareTo");

I don't get any error saying that I can't implement Comparable again. But in my testing, I can't get back to Animal's compareTo method once I create a PolarBear, even though PolarBear should inherit the Animal's compareTo. I tested the following:

Animal bobo = new PolarBear();
bobo.compareTo(null);
((PolarBear) bobo).compareTo(null);
((Animal)bobo).compareTo(null);
((Comparable) bobo).compareTo(null);    

And each of them printed:

 PolarBear.compareTo

Is there any way to access the Animal 's compareTo ? Logically, wouldn't I want the ability to be able to compare the animal qualities of a PolarBear , as well as the PolarBear qualities?

It doesn't matter that you are casting your bobo object to PolarBear , Animal , or Comparable , the run-time type of the object will always be PolarBear since you instantiated it as

Animal bobo = new PolarBear();

Therefore whenever you call .compareTo(wtv) it'll call the PolarBear implementation.

This is what Polymorphism is. Read up on late binding as that's how polymorphishm is implemented in java.

If you want to call the compareTo(wtv) method of your parent classes, then you have to do super.compareTo(wtv) inside any other subclass' methods.

compareTo and inheritence is generally a tricky business . in theory, having subclasses to a Comparable concrete superclass breaks the comparable contract. for example - suppose i have a superclass A that compares to other A's by checking fields a1 and a1. now lets add a subclass B that has an extra field b1. anA.compareTo(aB) would compare by a1 and a2 while ((A)aB).compareTo(anA) would not do the same
as to your question - you can call the Animal version of compareTo only from the subclass itself, by doing something like

super.compareTo(<something>)

but not from "outside", as you've overridden the method. it is your responsibility to maintain the same "contract" to the outside world as the method you've just overridden.

您始终可以从PolarBear中调用super.compareTo(arg0)来调用Animal.compareTo中的逻辑。

It's very simple. the underlying object is of PolarBear class and you are changing the interface (Class that is used to hold the reference of the object) of the object every time.

JVM check at compile time whether a method in the interface (Class which is used to hold the reference of the object) exist or not but binding takes place at run time because Child (PolearBear) has that method so it will always run that.

use super.compareTo() in order to call the parent method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM