There are classes Document , Element , Elements . I created the heir classes:

 Source extends Document SourceElement extends Element SourceElements extends Elements 

Moreover, the Document class is a heir from Element , and Elements is a heir from ArrayList<Element> .

The Element class has a select method that Elements returns. But I need SourceElement .

How do I organize inheritance so that I can write my methods without any problems? For example, now I have to write the select method, which returns SourceElements , I have to perform castes. In addition, you cannot use the name of the select method. And I would love to.

    1 answer 1

    In an inherited class, you can override the parent's method with the specification of the return value.

     public class Elements {} public class SourceElements extends Elements {} public class Element { Elements select() { ... } } public class SourceElement extends Element { @Override SourceElements select() { ... } }