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

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









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 connect my database to my application ?
How can I protect my database password ?
How can I find my DHCP IP address on my computer?
How can i modify my account in roseindia
how can i use toggle button in my application
my hibernate showing the following exception so how i can resolve it
How i can send testing mail on my id using java?
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
How can I start my career in data science?
i am unable to identify the error in my code
i am unable to identify the error in my code
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
I want to change my user name on your website ,how can i do this
Can I put online courses on my CV?
Can I put coursera on my resume?
Can I learn Hadoop on my own?
Can I learn R on my own?
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
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
How can I Convert my Image Files to Text Files? - IDE Questions
how i open my web page
How could I use Socket in my application?
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?
Average Age of my Class?
this is my javascript code and i am not understanding the mistake in this,please help me?
Small Bug How to solve in my code
How do I launch my career as Data Analyst?
how to render my htmlstring from java class in velocity template?
How much time will it take to learn machine learning on my own?
How do I install urllib2 library in my Python module
how to send my system.out.println in a class file to a jsp in Struts2
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?
how do i make a phone call from my app without quitting my application
Solve my code
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly
I GOT ERROR FOR MY PROJECT
I have problem in my Project
how do i use sql like query in my jsp page
how to display output on jsp from while loop of action class....actually i am retreiving the post from posts column from my sql.plz help.thnkss
Calculate an average Age of my Class?
Pls review my code - Struts

Ads