0

I have superclass A, which is extended by subclasses B1 and B2. Then, I have five subclasses (C1, C2, C3, C4, C5) that extend either B1 or B2.

I am trying to make an array containing one of each of these five subclasses.

These objects are all instantiated as instances of type A.

ClassA[] objects = new ClassA[5];

I attempt to reassign each of the objects to one of the subclasses:

objects[0] = new ClassC1;
objects[1] = new ClassC2;  // etc...

At this point, any methods that existed in Class A work fine, but methods defined in B1/B2 or the other subclasses are not found:

objects[0].MethodFromC1();   // returns a "symbol not found" error 

The instanceof keyword indicates that objects[0] is an instance of classes A, B1/B2, and C1.

What can I do to maintain my array of class objects (to loop through and perform operations), while getting my code to recognize the methods of the subclasses?

4
  • Would it be feasible to create a common (or abstract) method in ClassA that can be implemented / overwritten by the subclasses? That way, you need not worry about which object is of which concrete type, you simply invoke your generic doSomething method, and the individual objects will behave appropriately for their type. Commented Oct 9, 2013 at 1:42
  • 1
    As @Teeg says, you want an abstract base class, and override the desired methods in the derived classes. Commented Oct 9, 2013 at 1:59
  • 1
    You want to be able to create heterogenous containers, and avoid the strong typing of Java, but you just need to think polymorphism. This and other reasons why I find myself writing more Ruby (scala, clojure, any lisp variant would suffice). Commented Oct 9, 2013 at 2:02
  • What you say would work, but I'm trying to not have any methods in the subclass that aren't used. I have a few abstract methods in my Class A, but the methods in Class B1/B2 are more specific and wouldn't apply to all the subclasses. Commented Oct 9, 2013 at 2:12

4 Answers 4

3

If you are doing a lot of instanceof and conditional logic based on class, you are completely missing out on the benefits of an object-oriented language. Just stick to C.

You should have some method do(), for example, that is abstract in ClassA but implemented in ClassB and ClassC. Then you iterate over the array and call do() on every object in there. The polymorphic call will result in the right do()'s being called.

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Elements in the objects array don't know anything about ClassC1 since they're only guaranteed to be members of ClassA.

For instance, if you have a class hierarchy of Animal and subclass Cat and its subclass Lion, you're trying to call the Animal.maimSafarigoer() method. Animals in general don't know anything about safarigoers, only Lions know how to do that.

Use the instanceof operator to check if you're operating on a particular subtype.

(Sorry for the gruesome analogy. :-) )

Comments

1

You're trying to implement variants in Java. This subject has long been one of the things I hate the most about this language.

http://jazzjuice.blogspot.com/2010/10/6-things-i-hate-about-java-or-scala-is.html

I have listed about 8 suboptimal ways to do variants there.

Comments

1

You can use the instanceof keyword in the if statement and cast the object to the desired type. For example,

for (ClassA obj : objects) {
    // do something common...

    if (obj instanceof ClassC1) {
        ClassC1 c1Obj = (ClassC1) obj;
        c1Obj.MethodFromC1();
    }
}

2 Comments

When trying to implement as a foreach loop I receive an error stating that the variable (c10bj equivalent) may not be initialized. I know that for more primitive types the solution is to just set the variable explicitly (int x = 0, etc), but I'm unsure how to proceed with objects.
Sorry. There was a mistake in the code, which is fixed now. You should type cast the obj variable to ClassC1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.