Aakeef
My Base Class is Changing every time in my code. How I can overcome this?
0 Answer(s)      2 years and 4 months ago
Posted in : Java Interview Questions

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

public 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(); 
}
}

And I want to have Generator pick up a new GeneratorBase at run time, so change to

public class GeneratorBase2 {
public GeneratorBase2(){
System.out.println("Generator Base 2 is used");
}
}

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 ...." portion

Advertisement
View Answers









Related Tutorials/Questions & Answers:
Advertisements