My Base Class is Changing every time in my code. How I can overcome this?

Ads
 

My Base Class is Changing every time in my code. How I can overcome this?

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

View Answers

Ads









Related Tutorials/Questions & Answers:
My Base Class is Changing every time in my code. How I can overcome this?
How can i modify my account in roseindia
Advertisements
How can I find my DHCP IP address on my computer?
my hibernate showing the following exception so how i can resolve it
how can i use toggle button in my application
How i can send testing mail on my id using java?
How can I start my career in data science?
I want to change my user name on your website ,how can i do this
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
How to disable Browser's back button (at the same time i want my own back button to be enabled) :p
How to disable Browser's back button (at the same time i want my own back button to be enabled) :p
Can I put coursera on my resume?
Can I put online courses on my CV?
Can I learn Hadoop on my own?
Can I learn R on my own?
i am unable to identify the error in my code
i am unable to identify the error in my code
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
How can I open my Java desktop app by clicking on its file?
how i open my web page
How could I use Socket in my application?
this is my code but i cant able to run it wt is the problem
this is my code but i cant able to run it wt is the problem
Small Bug How to solve in my code
How much time will it take to learn machine learning on my own?
how to render my htmlstring from java class in velocity template?
How can i pass the valus from a JSP to the action class???
how to send my system.out.println in a class file to a jsp in Struts2

Ads