There is an abstract class, and in it the getSmallClone(double i) method getSmallClone(double i) which should return an object of such an abstract class (its successor).

The compiler wants me to immediately implement an abstract method, but this cannot be done. How to be?

 abstract class AbstractFigure { abstract boolean inFigure(Point point); AbstractFigure getSmallClone(double i) { return new AbstractFigure(st, en); } } 
  • 3
    The reason is that you cannot create an abstract class object. It is hampered by the fact that there is an abstract method that must be defined before creating an object, respectively - Chubatiy
  • ru.wikipedia.org/wiki/… ? - Slava Semushin
  • I think you can use generics and design type <? super E> <? super E> (where AbstractFigure will be as E) to implement what you want to do. - DimXenon
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

This is really impossible, an instance of an abstract class cannot be created.

You must in some way return a particular child class in which the abstract method will get implementation.

To do this, you must either make the getSmallClone method also abstract (and implement it in the heirs), or create a derived class "in place":

 return new AbstractFigure(st, en) { @Override boolean inFigure(Point point) { // тут какая-нибудь подходящая имплементация, например: return AbstractFigure.this.inFigure(point); } }; 

Choose the path that is best for your program. Based on the name getSmallClone , you may need a second way. Then decide for yourself.

  • either make it static and create successors in it - Grundy

An abstract class in object-oriented programming is a base class that does not involve creating instances.

Wikipedia

 public abstract class AbstractFigure { protected abstract AbstractFigure getSmallClone(double i); } public class Figure extends AbstractFigure { @Override public Figure getSmallClone(double i) { return /*... */; } }