-1

I have problem with the below code in Java 8. In Java 7, the code works fine but when I changed it to Java 8 it shows this error:

The method mymethod(arg1,context,arg2) is ambiguous for the type Z.

I tried to use AbstractContext<MyModel> while calling the method, in Eclipse the error disappeared but while compiling it still occurs. Does anyone have any idea what could be wrong?

public interface K<T extends MyModel>{
AbstractContext<T> create(T context, SomeObject arg1, SomeObject1 arg2);
}

public abstract class X {

@Override 
public AbstractContext<MyModel> create(final MyModel context,final SomeObject1 arg1,SomeObject2 arg2){
//somelogic
}
   protected String mymethod(final SomeObject arg1,
          final AbstractContext<MyModel> context, final SomeObject1 arg2){
      //some logic 
      return "test";
   }
}

public class Y extends X implements Z<MyModel>{

@Override 
public AbstractContext<MyModel> create(final MyModel context,final 
SomeObject1 arg1,SomeObject2 arg2){
//somelogic
}
   protected String mymethod(final SomeObject arg1,
           final AbstractContext<MyModel> context, final SomeObject1 arg2){
      return super.mymethod(arg1,context,arg2);
   }
}

public class Z extends Y {
   protected void somemethod(arg1,context,arg2){
     //some logic
      this.mymethod(arg1,context,arg2); 
    }//here error occurs


@Override
protected String mymethod(final SomeObject arg1,
           final AbstractContext<MyModel> context, final SomeObject1 arg2){
      //some new logic 
      return "newtest";
   }
}
  • I think you are missing bracket in one method – Ravindra Kumar Jul 30 '18 at 09:34
  • 2
    Where does `arg1`, `context`, `arg2` in method `somemethod` come from? You need these as parameters to call `mymethod` but they neither fields of `Z` nor `Y` nor `X`. It looks like as you would have to set these as parameters of `somemethod` too or to define them within `somemethod`. And if this is the case I doubt it's an error which comes with Java 8. – LuCio Jul 30 '18 at 09:37
  • I've just tried this with `SomeObject arg1 = null; SomeObject1 arg2 = null; AbstractContext context = null;` and it compiles just fine. voting to close... – Eugene Jul 30 '18 at 09:41
  • Please provide exact code.. this error is related to arguments provided with method while calling. so you need to provide what SomeObject, SomeObject1, MyModel refers here... – DhaRmvEEr siNgh Jul 30 '18 at 09:44
  • I would make sure you have the latest update of Java 8. – Peter Lawrey Jul 30 '18 at 10:15
  • I corrected brackets and @Override mymethod is in Z class. – Adam Płaszczyk Jul 30 '18 at 11:48
  • I added more code to show it more clearly I hope – Adam Płaszczyk Jul 30 '18 at 11:59
  • I'm sure that problem comes from change Java 7 to 8 because this worked earlier – Adam Płaszczyk Jul 30 '18 at 12:01
  • I get no error here. `this.mymethod(arg1,context,arg2);` in class `Z` jumps to `protected String mymethod(final String arg1, final AbstractContext context, final Integer arg2)` in class `Y`. – Chris311 Jul 30 '18 at 14:36

1 Answers1

0

This code you provided isn't complete or it's too ambiguous ,I didn't understand what are you trying to do ?

That is your first class and it's normal (runs on jdk7,8,9,10&11)

public abstract class X {
   protected String mymethod(final SomeObject arg1,final 
   AbstractContext<MyModel> context,final SomeObject1 arg2){
      //some logic 
      return "test";
   }
}

and that is your second class :

public class Y extends X {
   protected String mymethod(final SomeObject arg1,final 
   AbstractContext<MyModel> context,final SomeObject1 arg2){
      return super.mymethod(arg1,context,arg2);
   }
}

fast tip about it Don't override a method if you don't want to change it's behavior.

now the third class :

public class Z extends Y {
   protected void somemethod(){
      this.mymethod(arg1,context,arg2); 
    }//here error occurs
}

There are some variables that you used but you didn't define -or even initialize- them So ofcourse there is an error in this line that you used them, and i tested same code using jdk7 and the same error appears.

last code which is not a class but a method that doesn't belong for any one of the classes above :

@Override
protected String mymethod(final SomeObject arg1,final 
   AbstractContext<MyModel> context,final SomeObject1 arg2){
      //some new logic 
      return "newtest";
   }

you can't create a method in java that don't belong to any class.

(Hope you to learn more about java before using different versions of jdk)