3

I want to create a new instance depending on an object, where I have the super class variable. Is this somehow possible without implementing a getNew() function or without usage of an ugly if chain? In other words: How to implement the following newSubClass(..) function without using the getNew() function?

public abstract class SuperClass {
    abstract public SuperClass getNew();
}

public class SubClassA extends SuperClass {
    @Override
    public SuperClass getNew() {
        return new SubClassA();
    }
}

public class SubClassB extends SuperClass {
    @Override
    public SuperClass getNew() {
        return new SubClassB();
    }
}

private SuperClass newSubClass(SuperClass superClass) {
    return superClass.getNew(); 
}
1
  • Polymorphism is the cleanest solution for this problem and you already have it implemented. When Java Reflection appears to be the solution, I question very hard whether it really is. Reflection sidesteps the type system and makes static analysis and automated refactoring harder or impractical. Saving code doesn't balance well against losing refactoring. Code is mainly valuable to a business for its ability to change to meet new requirements not in its ability to be short. Commented Jul 3, 2015 at 14:16

2 Answers 2

1

After having some time to think about and zv3dh's contribution I decided this second answer.

I'am getting now you want an new instance of an instance of a subclass' type of SuperClass without knowing the concrete sub-type at runtime.

For that you have "reflexion".

public abstract class A_SuperClass {
    public A_SuperClass createNewFromSubclassType(A_SuperClass toCreateNewFrom) {
        A_SuperClass result = null;
        if (toCreateNewFrom != null) {
            result = toCreateNewFrom.getClass().newInstance();    
        }
        // just an example, add try .. catch and further detailed checks
        return result;
    }
}

public class SubClassA extends A_SuperClass {

}

public class SubClassB extends A_SuperClass {

}

If you search for "java reflexion" you will get lots of results here on SO and on the web.

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

2 Comments

You are a very wise gentleman! No need for the cast to SuperClass though.
Thank you Klaus. Glad you got your answer. Removed the cast.
0

Have a look at the "FactoryMethod" design pattern.

It is exactly what you are looking for: It does encapsulate the "new" operator.

However your example makes me wonder:

  • Your getNew() reimplements what the constructor would do anyway

Try something like this:

public abstract class SuperClass {
    public SuperClass createSuperClass(object someParam) {
        if (someParem == a) return new SubClassA();
        if (someParem == b) return new SubClassB(); 
    }
}

public class SubClassA extends SuperClass {

}

public class SubClassB extends SuperClass {

}

As you see you need some IF at some place ...

Comments

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.