简体   繁体   中英

Java List of objects inheritance issues

I have a class called Interpreter. It is inherited by a few classes, including a class called LoadStep.

I have created a list of objects:

public static List<Interpreter> fileActions = new ArrayList<Interpreter>();

Objects were added in this way:

if (actionName.equals("LOAD")) {
    LoadStep loadAction = new LoadStep();
    fileActions.add(loadAction);
}

When trying to access my objects and call their methods, I am unable to access the child class methods. I was expecting to, because when I try getClass() to see the object class, I get the child class name.

    for (int i = lineNumber; i < Test.fileActions.size(); i++) {

        System.out.println(Test.fileActions.get(i).getClass());
        if (Test.fileActions.get(i) instanceof LoadStep) {
            LoadStep newAction = Test.fileActions.get(i);
            myMemoryAddress = Test.fileActions.get(i).getMemoryAddress(); //This line gives me Cannot Find Symbol, as getMemoryAddress is LoadStep's method, and not Interpreter
        } 
    }

The print statement gives me:

class test.LoadStep

You'd need to typecast your LoadStep instances, like

Interpreter step = ...;
if (step instanceof LoadStep) {
  LoadStep sub = (LoadStep) step;
  sub.invokeSubclassMethod(...)
}

This is nature of Polymorphism, when you assign a child object to a parent object, you can access just parent methods and attributes via parent object.

Child child = new Child ();
Parent parent = child;

To upcast a Child object, all you need to do is assign the object to a reference variable of type Parent. The parent reference variable cannot access the members that are only available in Child.

Because parent references an object of type Child, you can cast it back to Child. It is called downcasting because you are casting an object to a class down the inheritance hierarchy. Downcasting requires that you write the child type in brackets. For example:

Child child2 = (Child) parent;

Java is a strongly typed language and early binding. So that means the compiler is setting the visibly of the variable based on how its declared.

Example:

public class MyClass {
    public void myMethod(){}
    public void myMethod2(){}
}
public class MyClass2 extends MyClass{
    public void myMethod3(){}
}
public class MyTestClass {

    public void doSomeThing(){

        MyClass myClass= MyClass2();
        myClass.myMethod(); // valid
        myClass.myMethod2(); // valid
        myClass.myMethod3(); // invalid
    }
}

You can't call the method myMethod3() on the variable as it was typed to its parent/ancestor. This is the problem you are having. If you want to access the method you have to typecast it to the actual class like this:

((MyClass2)myClass).myMethod3(); // valid

which requires you to know the type you want it to be cast as, and basically destroys any polymorphism. Avoid using inheritance arbitrarily. Favor interfaces over inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM