0


I am checking a boolean condition passed into a multi-class' constructor, and if true I'm essentially trying to assign this to an instance of the sub-class.

That doesn't work of course, so is there any way to re-direct an instance in this way? If there no way to do this, what are the reasons why?


Here is the multi-class:
public class MultiClass {

    String str;

    public MultiClass(boolean flag) {
        if (flag) {

            // doesn't work! wish it would:
            // this = new ExtraClass();

        } else {
            this.str = "you are in the first class";
        }
    }
} // end of MultiClass

class ExtraClass {

    String str;

    ExtraClass() {
        this.str = "you are in the second class";
    }
} // end of ExtraClass


...and the tester class:

public class MultiClassTest {

    public static void main(String[] args) {
        MultiClass test = new MultiClass(false);
        System.out.println(test.str);
    }
} // end of MultiClassTest


Ian Campbell
  • 2,678
  • 10
  • 56
  • 104

2 Answers2

3

You can have either use a factory method e.g. a static method which can return any implementation to create different implementations, or use delegation to delegate to the actual implementation. (The constructed class would be just a wrapper)

Note: a constructor doesn't create an instance, it only initialises an instance which has been created for you. You can't change it's type once created.


A simpler approach is to have one class which checks the flag to determine what it should do. (with an if statement in your code)

public class MultiClass {
    final String str;

    public MultiClass(boolean flag) {
        if (flag) {
            this.str = "you are in the second class";
        } else {
            this.str = "you are in the first class";
        }
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks @Peter for the help! I've found some good info about the factory method pattern here: http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java ...and about the delegation pattern here: https://en.wikipedia.org/wiki/Delegation_pattern#Simple. – Ian Campbell May 04 '13 at 07:24
0


As Peter has suggested here, I have discovered how to use the "factory method" design-pattern, and am posting my code for posterity.



Here is a parent class with the factory method:
public abstract class Parent {

    /*
     * "factory method" design-pattern:
     */
    public static Parent getInstanceOf(boolean flag) {
        if (flag) {
            return new MultiClass();

        } else {
            return new ExtraClass();
        }
    }
} // end of Parent


...here is the multi-class, which extends from the Parent class:

public class MultiClass extends Parent {

    String str;

    public MultiClass() {
        this.str = "you are in the first class";
    }
} // end of MultiClass

class ExtraClass extends Parent {

    String str;

    ExtraClass() {
        this.str = "you are in the second class";
    }
} // end of ExtraClass


...and here is the tester class with the main method:

public class MultiClassTest {

    public static void main(String[] args) {

        boolean flag = false; // try changing to true
        Parent test = Parent.getInstanceOf(flag);

        if (test instanceof MultiClass) {
            System.out.println( ((MultiClass)test).str);

        } else if (test instanceof ExtraClass) {
            System.out.println( ((ExtraClass)test).str);
        }
    }
} // end of MultiClassTest



Note the useage of instanceof and the explicit casting.
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104