I've researched this issue a great deal, but have not been very successful in finding a solution close to my situation. I apologize if I am redundant.

Anyway, I am new to generics and so I'm not entirely clear on the syntax. But here is my situation:

I have an Interface (let's call it IService) and an implementation (ServiceImpl). The implementation class has an abstract method that is to be overridden by an extending class (ServiceExt).

I have an abstract bean (let's call it AbstractBean) which has a few fields and is meant to be extended by another bean (BeanExt) in order to add some more fields.

Ok, now that the situation is laid out, here is my abbreviated code:

 

Interface:

public interface IService <B extends AbstractBean> {
    B method(B bean, Object data) throws Exception;
}

Implementation:

public abstract class ServiceImpl <B extends AbstractBean> implements IService<B> {
    public abstract B method(B bean, Object data);
}

Abstract Bean:

public abstract class AbstractBean{

    private String fname;

    private String lname;

    [getters and setters]
}

Extended Bean:

public class BeanExt extends AbstractBean{

    private String phoneNum;

    [getter and setter]
}

Extending Class: aka Class giving me issues

public class ServiceExt <B extends AbstractBean> extends ServiceImpl <B> {

    @Override
    public <B> BeanExt method(Bbean, Object data) {
        if(beaninstanceof AbstractBean){
            BeanExt beanExt = (BeanExt) session;
            beanExt.setFname("John");
            beanExt.setLname("Doe");
            beanExt.setPhoneNum("123456789");
            return beanExt;
        }else{
            return null;
        }
    }
}

Ok, so now eclipse is giving me all kinds of non-descriptive issues and I don't know what to do. I've played around with the extending class methods a ton but have not found success. Above is how it looks after my last tinkering.

The issues currently presented by my IDE within the class ServiceExt are:

  • The method method(B, Object) of type ServiceImpl must override or implement a supertype method
  • The type ServiceExt must implement the inherited abstract method [some other non-abstract method in the interface]

Edit: For further clarification, what I am ultimately trying to do is:

  • Create a bean which extends the AbstractBean and add another field
  • Define the abstract method inside the class ServiceExt
  • Within the method, set some values in the extended bean
  • Return the extended bean with its new values

Update - I've taken all of your input and the changes are reflected below. The issue currently presented by my IDE within the class ServiceExt is:

  • The type ServiceExt must implement the inherited abstract method [some other non-abstract method in the interface]

Interface:

public interface IService <B extends AbstractBean> {
    B method(B bean, Object data) throws Exception;
    B otherMethod(HttpServletRequest request) throws Exception;
}

Implementation:

public abstract class ServiceImpl <B extends AbstractBean> implements IService<B> {
    public abstract B method(B bean, Object data);

    @Override
    public final B otherMethod(HttpServletRequest request) {
        return [something]; 
    }
}

Abstract Bean:

public abstract class AbstractBean{

    private String fname;

    private String lname;

    [getters and setters]
}

Extended Bean:

public class BeanExt extends AbstractBean{

    private String phoneNum;

    [getter and setter]
}

Extending Class: aka Class giving me issues

public class ServiceExt extends ServiceImpl <BeanExt> {

    @Override
    public BeanExt method(BeanExt bean, Object data) {
        BeanExt beanExt = (BeanExt) session;
        beanExt.setFname("John");
        beanExt.setLname("Doe");
        beanExt.setPhoneNum("123456789");
        return beanExt;
    }
}
有帮助吗?

解决方案

You are not really overriding the method in super class. Here's the overridden method:

@Override
public BeanExt method(BeanExt bean, Object data) {
        BeanExt beanExt = (BeanExt) session;
        beanExt.setFname("John");
        beanExt.setLname("Doe");
        beanExt.setPhoneNum("123456789");
        return beanExt;
}

Since you're extending from ServiceImpl<BeanExt>, the type parameter B is replaced with BeanExt type, so does the return type and parameter of the method.

Also note that I've removed the instanceof check, as it is really not needed. bean will always be an instance of AbstractBean, because that is enforced by the bound you gave to the type parameter declaration of ServiceImpl class.

其他提示

I did not entirely understand what you're trying to do, but is this acceptable/working?

public class ServiceExt <B extends BeanExt> extends ServiceImpl <B> {

    @Override
    public B method(B bean, Object data) {
        bean.setFname("John");
        bean.setLname("Doe");
        bean.setPhoneNum("123456789");
        return bean;
    }
}

I just corrected all the errors:

interface IService <B extends AbstractBean> {
    B method(B bean, Object data) throws Exception;
}

abstract class ServiceImpl <B extends AbstractBean> implements IService<B> {
    public abstract B method(B bean, Object data);
}

abstract class AbstractBean{

    String fname;
    String lname;
}

class BeanExt extends AbstractBean {

    String phoneNum;
}

class ServiceExt <B extends AbstractBean> extends ServiceImpl <B> {

    @Override public B method(B bean, Object data) {
        if (bean instanceof BeanExt) {
            BeanExt beanExt = (BeanExt) bean;
            beanExt.fname = "John";
            beanExt.lname = "Doe";
            beanExt.phoneNum = "123456789";
            return bean;
        }else{
            return null;
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top