I have had the below question asked in interview, i'm curious to learn the answer.
I have a base class, 'GeneratorBaseClass' that is extended by Generator. The question I was asked was about creating a new base class 'GeneratorBaseClass2' and having Generator change to extend that at run time (without having to change Generator). So, as an example of the code
And I want to have Generator pick up a new GeneratorBase at run time, so change topublic class GeneratorBase1 { public GeneratorBase1(){ System.out.println("Generator Base 1 is used"); } } public class Generator extends GeneratorBase1{ public Generator() { //will call the appropriate super class } public static void main(String[] args){ Generator test=new Generator(); } }
The form of Generator can be changed, but must not be changed every time the base class changes. This is about allowing the selection of base class at runtime and I don't want to just change the "extends ...." portionpublic class GeneratorBase2 { public GeneratorBase2(){ System.out.println("Generator Base 2 is used"); } }