简体   繁体   中英

java abstract class for inheritance

I have a project where I am currently loading the records from the excel file to the database using from org.apache.poi

I have 3 kinds of files that I am loading into different Dto classes. Two of these dtos shares the same base class (they have common attributes)

@Getter
@Setter
@ToString
public class PpyRecordDTO {

private String units;
private Double quantity;

}

@Getter
@Setter
@ToString
public class FirstRecordDTO extends RecordDTO {
private String normDescription;
private String relatedUnits;

}


@Getter
@Setter
@ToString
public class SecondRecordDTO extends RecordDTO{

private String normName;
}

@Getter
@Setter
@ToString
public class ThirdRecordDTO {

private String code;
}

The ThirdRecordDto class has a unique attribute and no shared attributes with the base dto class RecordDTO

I want to return from this method the base class: RecordDto (But ThirdRecordDTO cannot extend it since there are no common fields)

    public static List<?extends RecordDTO> readPpyExcelFile(MultipartFile file, SourceType sourceType){
    //TODO: making readPpyExcelFile generic
    try {
        Workbook workbook = WorkbookFactory.create(new BufferedInputStream(file.getInputStream()));

        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rows = sheet.iterator();

        List<? extends RecordDTO> lstRecords = processRecords(rows, sourceType);

        // Close WorkBook
        workbook.close();

        return lstRecords;
    } catch(ApiGenericException apiException){
        throw new ApiGenericException(apiException.getStatus(), apiException.getMessage(), apiException.getErrorType());
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new ApiGenericException(HttpStatus.INTERNAL_SERVER_ERROR,"Enable while loading the file", ApiErrorType.Unhandled
        );
    }
}

Is there a way to make the dto ThirdRecordDto also be returned or inherit from an abstract class which is shared by the other dtos in order to return the type <? extends List from this method?

In general you can go with something like this:

public interface Ppy {
 // common methods if any
}

public class PpyRecordDTO implements Ppy{...}
public class FirstRecordDTO extends PpyRecordDTO {...} // so that it also implements Ppy interface
public class SecondRecordDTO extends PpyRecordDTO {...} // the same as above
public class ThirdRecordDTO implements Ppy {...} // Note, it doesn't extend PpyRecordDTO but implements the interface

Now in the method, it's possible to:

 public static List<Ppy> readPpyExcelFile(MultipartFile file, SourceType sourceType){...}

This will work, however, you should ask yourself the following: what will do the code that will call this method, namely how it will differentiate between the different implementations? If the interface has a common method that makes sense for all implementations - fine, it will be able to call the method. For example, if it has a method like render(Page) or something, the code might be:

List<Ppy> ppis = readPpyExcelFile(...);
Page page = ...
for(Ppy ppi : ppis) {
    ppi.render(page);
} 

However, if the interface doesn't have any common methods - it won't help much. Inheritance is used when the child object can be viewed as a specialization of Parent (so that the child "is a" parent). So think whether inheritance is really appropriate here, assuming ThirdRecordDTO doesn't have anything in common with the rest of the classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM