error method overloaded in java

2

Hi, can someone help me? I do not know why I get this error.

abstract class Uno {
        protected int d1,d2;
        abstract public int devolver_suma (int x,int y);
}
class Dos extends Uno {
    public int devolver_suma(int x,int y) {
        d1=x+3;
        d2=y+3;
        return d1+d2;
    }

}
class Tres extends Dos {
    public int devolver_suma(int x,int y) {
        d1=x-3;
        d2=y-3;
        return d1+d2;
    }
    public int devolver_suma(int x,int y, int i, int j) {
        d1=x-i;
        d2=y-j;
        return d1+d2;
    }
}
class Principal {
    public static void main(String [] args) {
        int c=0,d=0;
        Uno []lista=new Uno[2];
        lista [0]= new Dos();
        lista [1]= new Tres();
        for (int i=0; i<2; i++){
            d=lista[i].devolver_suma(10,10);
            System.out.printf("d=%d \n",d);
            c=lista[i].devolver_suma(2,2,2,2); /*aqui me dice que que el metodo no es aplicable con esos argunmentos, osea que no se me sobrecarga.*/
            System.out.printf("c=%d \n",c);
        }
    }
}
    
asked by gabriel gomez 16.05.2016 в 21:08
source

5 answers

1

The problem is given to you because lista[i] in the first iteration of for is an instance of class Dos . This causes the call to the method devuelve_suma with four error parameters, since the class Dos does not have this method declared, it only has it with two parameters.

You should check the class to which lista[i] belongs to solve this. Of course, for this example program. In a real application you should not group objects of different classes in an array, or at least not perform these operations (overloading methods and traversing the array).

You can fix it with this code:

for (int i = 0; i<2; i++) {    
  d=lista[i].devolver_suma(10,10);
  System.out.printf("d=%d \n",d);
  if(lista[i] instanceof Tres) {
    c=lista[i].devolver_suma(2,2,2,2);
    System.out.printf("c=%d \n",c);
  }
}
    
answered by 17.05.2016 в 09:58
1

_. /

The solution is very simple.

The problem is given to you because lista[i] in the first iteration of the for is an instance of the class Two. This causes the call to the devuelve_suma() method with four error parameters, since class Two does not have this declared method, it only has two parameters.

The error is the iteration of the list.

Good luck! : D

    
answered by 10.06.2016 в 18:52
0

You must fix it with the following line:

c=((Tres)lista[i]).devolver_suma(2,2,2,2);

Why?

  

Because the abstract class that you are inheriting does not have that number of   parameters, does not have that function at that level that you try.

    
answered by 16.05.2016 в 21:19
0

The problem is that your array is declared as if the variables were of type Uno , here:

Uno[] lista = new Uno[2];

In Java, when working with a variable, only members (attributes and methods) that are in the class type defined for the variable can be accessed. The class Uno does not have a method with the signature int devolver_suma(int, int, int, int) , therefore the compile error. It does not matter if you initialize the elements of the array as if they were of the class Dos or Tres , the compiler only understands that the elements of lista are of type Uno .

To solve this, the best alternative is to use downcasting. That is, a defined variable of class type behaves as a class of a type that inherits or extends from that class. In this case, it would be that the variables in your Uno[] lista array behave as a specific type Dos or Tres . To ensure that the correct downcast is done, the instanceof operator can be used:

if (lista[i] instanceof Tres) {
    c=lista[i].devolver_suma(2,2,2,2);
    System.out.printf("c=%d \n",c);
}
c=lista[i].devolver_suma(2,2,2,2); /*aqui me dice que que el metodo no es aplicable con esos argunmentos, osea que no se me sobrecarga.*/

The overload works correctly, but it is only for the devolver_suma method defined in class Tres . The overload of methods (overloading) works on a single class, while the inheritance and polymorphism of methods works between the ancestor class and its descendants.

    
answered by 17.05.2016 в 16:56
-1

The error is given because the function will only work with that signature if and only if i = 1 ( lista [1]= new Tres() ), the first time you enter the cycle i = 0 ( lista [0]= new Dos() ), at that level there is no definition of your function with that signature, so it raises an exception.

Basically the only way it will work is like this:

c=lista[1].devolver_suma(2,2,2,2);

Another option would be making the cast explicit:

c=(Tres)lista[0].devolver_suma(2,2,2,2);
    
answered by 16.05.2016 в 21:18