Overriding extended class comparison - what happens?

I am trying to override compareTo like this: This is the original:

@Override
public int compareTo(ProductPart6 s)
{
    return this.getproductName().compareTo(s.getproductName());

}

this is what I am trying to override with: it throws an error The compareTo (SubClass) method of type SubClass should override or implement the supertype method.

@Override
    public int compareTo(SubClass s)
    {
        return this.getTitle().compareTo(s.getTitle());

    }

I think this is very wrong. My ProductPart6 does not have getTitle () and calls it

@Override
    public int compareTo(ProductPart6 s)
    {
        return this.getTitle().compareTo(s.getTitle());

    }

to throw an error (getTitle () - undefined for type ProductPart6) - if I defined it there, there would be no redefinition of it. What am I doing wrong? I have a SubClass extending ProductPart6, and ProductPart6 implements Comparable - I thought I could implement it on SubClass, but no. This is not an option.

+4
2

Comparable<T>, (T), . compareTo T.

:

public class Alpha implements Comparable<Alpha> {
    @Override
    public int compareTo(Alpha a) {
        ...
    }
}

...

public class Bravo extends Alpha {...}

compareTo Bravo, Alpha, Alpha - , T ( implements Comparable ). , , , @Override, .

, , , ProductPart6 SubClass, . , Comparator , T .

+3

@ATG: :

public class Alpha implements Comparable<Alpha> {
    @Override
    public int compareTo(Alpha a) {
        ...
    }
}

, Comparable, SortedSet<Alpha>. , SortedSet<Alpha>, Alpha , Bravo:

public class Bravo extends Alpha {...}

Alpha, Bravo, .

A.compare(B);

A - Alpha, B - Bravo. compareTo, Alpha.

B.compare(A);

A Alpha, B Bravo.

@Override
public int compareTo(Bravo b) { ... }

Bravo, B.compare(A). :

@Override
public int compareTo(Alpha a) { ... }

, Bravo Alpha. , Alpha , Bravo, , compareTo , .

, , Alpha , Bravo, - , ( , , @ATG ).

+2

All Articles