1

I have a class A which implements Comparable interface, and a class B which extends A. I need to find a way to overwrite compareTo in class B.

I found a similar question Cannot use comparable with father-son-grandson inheritance And tried to implement offered solution

Class A:

public class A<E extends A> implements Comparable<E>{
    String surname;
    int age;
      A(String surname, int age){
        this.surname = surname;
        this.age = age;
    }

   public  int compareTo(E o) {
        int res=0;
        res =this.surname.compareTo(o.surname);
        if(res==0)
            res = this.age-o.age;
        return res;
}

Class B:

public class B extends A<B> {
    int num;

    Student(String surname, int age, int num){
        super(surname,age);
        this.num = num;
    }
@Override
   public int compareTo(B o){
        int res=0;
        res =this.surname.compareTo(o.surname);
        if(res==0)
            res = this.age-o.age;
        if(res ==0){
            res= this.num-o.num;
        }
        return res;
    }

Exception:

Exception in thread "main" java.lang.ClassCastException: class T4.A cannot be cast to class T4.B (T4.A and T4.B are in unnamed module of loader 'app')

Sam
  • 11
  • 3
  • What code snippet do you get the classcastexception? This example works fine without such exception. Are you trying to cast like `B b = (A) a; – Laksitha Ranasingha Mar 29 '19 at 18:41
  • check [java.lang.ClassCastException ](https://stackoverflow.com/questions/3511169/java-lang-classcastexception), your code seems to work fine for me too, its maybe the way you are calling the classes that is wrong – Mischiefz Mar 29 '19 at 18:42

3 Answers3

1

Can you post the code which fails?

I just tried it on my own machine and it seems to work. The only time I could recreate the error was with following code: b.compareTo((B) a); Which can't work since you can't cast a parent type to its child type.

Also, I think it is just a copy error, but you are missing brackets in the provided code and constructor of class B does not match the class name.

Byte
  • 55
  • 1
  • 8
0

Your method in class B should be written as this instead:

@Override
public int compareTo(B o){
    int res=0;
    res =this.surname.compareTo(o.surname);
    if(res==0)
        res = this.age-o.age;
    if(res ==0){
        res= this.num-o.num;
    }
    return res;
}
  • Thanks for your comment, I added @Override but it didn't help – Sam Mar 29 '19 at 18:31
  • @Connor Ruggles, This is not going to help. The annotation has nothing to do with overriding in Java. That method has already been overridden. – Laksitha Ranasingha Mar 29 '19 at 18:37
  • @LaksithaRanasingha It won't solve the problem, but of course the annotation has something to do with overriding :/ – Dave Newton Mar 29 '19 at 18:48
  • @DaveNewton I don't think so. I was referring to the concept of override. It's a runtime operation and with or without an annotation, it stays the same. As long as method signature & return type are the same override works. So it doesn't. Of course, annotation provides compile time safety. – Laksitha Ranasingha Mar 29 '19 at 18:56
  • So it's related then. Saying it's "unrelated" is nonsense; it's *directly* related, as you stated. As i said, it's not *necessary*, but it's clearly related to overriding by definition. – Dave Newton Mar 29 '19 at 20:45
  • Picking words without understanding the whole doesn’t help @DaveNewton. – Laksitha Ranasingha Mar 29 '19 at 22:15
  • @LaksithaRanasingha Which is why I take issue with saying the Override annotation doesn't have anything to do with overriding. Saying an annotation explicitly about overriding isn't related is wrong, because it's imprecise (and because it's *only* about overriding), even if it isn't *required* to override a method. – Dave Newton Mar 29 '19 at 23:09
  • @LaksithaRanasingha ah my bad, I haven’t written java in a couple years and thought that was required for an override. – Connor Ruggles Mar 30 '19 at 19:28
0

Student != B

The class B need to have a contructor called B

FrV
  • 260
  • 2
  • 9