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 simplify my java code
Advertisements
How can I to my database to my application
How can I protect my database password ?
How can I connect my database to my application ?
How can i modify my account in roseindia
How can I find my DHCP IP address on my computer?
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
how can i use toggle button in my application
How i can send testing mail on my id using java?
my hibernate showing the following exception so how i can resolve it
I want to change my user name on your website ,how can i do this
How can I Convert my Image Files to Text Files? - IDE Questions
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
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?
i am unable to identify the error in my code
i am unable to identify the error in my code
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
Average Age of my Class?
How do I launch my career as Data Analyst?
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?

Ads