0

I have to work to a piece of code where there's this class with a bunch of static methods to be called from other classes.

I've created some new classes with an hierarchy like the following one:

public abstract class Superclass implements Comparable, Serializable
{
    //Superclass stuff
}

public class MidClass extends Superclass implements Comparable, Serializable
{
    //MidClass stuff
}

public class SubClass1 extends SuperClass implements Comparable, Serializable
{
    //SubClass1 stuff
}

public class SubClass2 extends MidClass implements Comparable, Serializable
{
    //SubClass2 stuff
}

Given that I need to call a different static method for every different subclass type, I've also added the following overloaded method to the first class:

public static objForMidClass elaborationMethod(MidClass midClass)
{
    //Stuff to do with MidClass obj
}

public static objForSubClass1 elaborationMethod(SubClass1 subClass1)
{
    //Stuff to do with SubClass1 obj
}

public static objForSubClass2 elaborationMethod(SubClass2 subClass2)
{
    //Stuff to do with SubClass2 obj
}

In the class from where I call the static methods I've something like

//Inside "object" and "inputList" Lists there's the correct type/subtype

List<SuperClass> inputList = (List<SuperClass>) object;

if (inputList != null && inputList.size() > 0)
{
    for(Iterator<SuperClass> it = inputList.iterator(); it.hasNext(); )
    {
        SuperClass item = it.next();

        //Next line gives the error: 
        //"elaborationMethod(MidClass) in the type classWithStaticMethods is not applicable for the arguments (Superclass)"

        AnotherClass retItem = classWithStaticMethods.elaborationMethod(item);

    {
}

Could you tell me why it doesn't recognize that the objects passed are all instances of subclasses of SuperClass and, hence, that it must search for the static method of the corresponding SubClass type?

Also, what could be a correct alternative way to model this situation?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ela
  • 325
  • 2
  • 10
  • It is not clear how `classWithStaticMethods` is created. – freedev Mar 10 '17 at 10:02
  • It's just the class "with a bunch of static methods" which I was talking about at the beginning of the question. The overloaded method I use is "elaborationMethod" and the declaration is showed in the second snippet of code – ela Mar 10 '17 at 10:05
  • 1
    Possible duplicate of [Overloaded method selection based on the parameter's real type](http://stackoverflow.com/questions/1572322/overloaded-method-selection-based-on-the-parameters-real-type) – Axel Mar 10 '17 at 10:08

1 Answers1

0

You should switch from overloading methods to overriding methods and move the static elaborationMethod methods into their respective classes.

At that point you can call elaborationMethod for each item in this way:

item.elaborationMethod();

Just to let you understand I have written this little example. Consider the printName method equivalent to your elaborationMethod.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Polymorphism {

  public static void main(String[] args)
  {
    List<SuperClass> inputList = new ArrayList<>();

    inputList.add(new SuperClass() {});
    inputList.add(new MidClass());
    inputList.add(new SubClass1());
    inputList.add(new SubClass2());

    for (SuperClass s : inputList) {
      s.printName();
    }

  }

}


abstract class SuperClass {

  public void printName()
  {
    System.out.println("SuperClass");
  }
}


class MidClass extends SuperClass {

  public void printName()
  {
    System.out.println("MidClass");
  }
}


class SubClass1 extends SuperClass {

  public void printName()
  {
    System.out.println("SubClass1");
  }
}


class SubClass2 extends MidClass {
  // Missing printName
}
freedev
  • 25,946
  • 8
  • 108
  • 125
  • Understood, but unfortunately I can't move elaboration logic into the classes themselves... – ela Mar 10 '17 at 11:29