imaginedrragon
imaginedrragon

Reputation: 303

Extending abstract classes and using same methods with different arguments

First of all, here's my abstract class:

public abstract class GeometricObject2D implements Comparable {
  public abstract boolean contains(GeometricObject2D g);
  public abstract boolean contains(double x, double y);
  public abstract boolean overlaps(GeometricObject2D g);
  public abstract int compareTo(Object temp);
  public abstract double getArea();
  public abstract double getPerimeter();

}

I'm supposed to extend it with two other classes, ComparableCircle2D and ComparableRectangle2D. Both contain all of the methods, except that they use their own arguments in the contains and overlaps methods, e.g:

public class ComparableCircle2D extends GeometricObject2D {
//...
  public boolean contains(ComparableCircle2D circle){
  //method
  }
//...
  public boolean overlaps(ComparableCircle2D circle){
  //method
  }
//...
}

This is the error I get:

Error:(1, 8) java: ComparableCircle2D is not abstract and does not override abstract method overlaps(GeometricObject2D) in GeometricObject2D.

Same goes for the other class. I don't fully understand what's wrong, so an explanation and a solution would be greatly appreciated.

P.S: The arguments have to stay the same.

Upvotes: 3

Views: 1686

Answers (4)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363449

You can't do it.

In your class you have to override all methods of the abstract class, then you have to override the method:

 @Override
 public boolean contains(GeometricObject2D g){...}

Hovewer you can use somenthing like this:

public abstract class GeometricObject2D<T extends GeometricObject2D> {
     public abstract boolean contains(T g);
}

And in your subclass:

public class ComparableCircle2D extends GeometricObject2D<ComparableCircle2D> {

    @Override 
    public boolean contains(ComparableCircle2D circle){
      //  return....;
    }
}

Upvotes: 2

highstakes
highstakes

Reputation: 1519

This is what generics are exactly for, do the following:

abstract class GeometricObject2D<T extends GeometricObject2D> {
  public abstract boolean contains(T g);
}

class ComparableCircle2D extends GeometricObject2D<ComparableCircle2D> {
  public boolean contains(ComparableCircle2D circle){
        return true;
  }
}

Upvotes: 2

Yassin Hajaj
Yassin Hajaj

Reputation: 21965

You must have the exact same arguments to override a method. Hence you're extending a substract class, all the abstract methods have to be implemented.

Here is a solution for you

public boolean contains(GeometricObject2D circle){
    if (!(circle instanceof ComparableCircle2D)) {
        throw InputMismatchException();
    } else {
    // method
    }
} 

Or (depending on the logic of your own code)

public boolean contains(GeometricObject2D circle){
    if (!(circle instanceof ComparableCircle2D)) {
        return false;
    } else {
    // method
    }
} 

Upvotes: 1

Ramanlfc
Ramanlfc

Reputation: 8354

your class must implement/override all methods of the abstract class and interface otherwise you'll have to mark it as abstract.

public boolean contains(ComparableCircle2D circle){

this is not a valid override as the contains() method in GeometricObject2D takes GeometricObject2D

Upvotes: 1

Related Questions