Search code examples
javainterfaceoverriding

Interface method have List<Objects>, but implemented class methods are List<MyClass>, which is not allowed


I am working on Interface which was implemented by different classes, but I am facing issues while I override the method in the classes.

Please help me defining proper method arguments in Interface which can be overridden by other class methods also

Interface I1
{
public doProcessing(List<Object> list);
}

Here are the classes

Class A implements I1
{

 public doProcessing(List<MyAbcclass> list)//This is not accepted 
  {
    //do processing
  }
}

Class B implements I1
{

  public doProcessing(List<MyXyzclass> list)//This is not accepted 
  {
    //do processing
  }
}

Solution

  • I think this is your wanted behavior:

    interface I1<T> {
        void doProcessing(List<T> list);
    }
    
    class A implements I1<MyAbcclass> {
    
    
        @Override
        public void doProcessing(List<MyAbcclass> list) {
    
        }
    }