Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

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

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

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

    You forgot to ask a question.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Hi Kevin,
    My problem is my 'generator' working fine/ remains same all the time,.Only my Base class is Changing, every time i need to update my code as,

    Class generator extends generatorbase1{}
    Class generetor extends generatorbase2{} and So on,.

    What should i write after 'extends..' keyword that my child class[generator class] will extends automatically from the Base class which i want.
    if i compile 'generator' & 'generatorbase1' it should extend from generatorbase1
    if i compile 'generator' & 'generatorbase2' it should extend from generatorbase2..and So on

    How can i tell in my code, that it should decide at compile time to extend.???

  4. #4
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 8 Times in 8 Posts

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

    I can't quite tell what you want, but I think you have it backwards. If Generator extends GeneratorBase1, then it cannot also extend GeneratorBase2. Either you would want GeneratorBase1 and GeneratorBase2 to extend Generator, or have them implement Generator. You can't extend more then 1 class at all, let alone change it at runtime.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

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

    Quote Originally Posted by theoriginalanomaly View Post
    I can't quite tell what you want
    I do not understand what you are trying to ask either. Can you try to explain it another way?

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

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

    My understanding is that if you write and compile "class Foo extends Bar {" that's it: game over. Foo will extend Bar, and Bar will be the immediate parent class of Foo for all time.

    @OP: If you have the exact wording of the question that might help. For instance it is possible that some specific functionality was desired and some other mechanism (not "extends") is possible.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by theoriginalanomaly View Post
    I can't quite tell what you want, but I think you have it backwards. If Generator extends GeneratorBase1, then it cannot also extend GeneratorBase2. Either you would want GeneratorBase1 and GeneratorBase2 to extend Generator, or have them implement Generator. You can't extend more then 1 class at all, let alone change it at runtime.
    i know it cannot extend more then 1 class, I'm not doing this at time.
    I have had the below question asked in interview,Lets clear it.
    I have a generator class which is extending Generatorbase1[Which has some values x=10,y=8], in version1 of my code.
    After some days my version change to version2, in that my [values x=5,y=3] values will change with the different class name Generatorbase2{}.
    in version 1 : Class generator extends Generatorbase1{...}
    In version 2, my entire generator class code will remains same only in need to update the existing code as
    Class generator extends Generatorbase2{...},.and So on my version's changing frequently. Every time need time update code for this small change,.How can i Overcome this??

    Can you suggest me any way to resolve this ???

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    A few important questions to ask (quite similar to each other, just slightly different):

    1. What is the lifetime of GeneratorBase{n}? If you've gone to using GeneratorBase3, are you going to keep and use GeneratorBase2 at the same time in your program? What about GeneratorBase1?

    2. Are there any other classes which need to have similar changes made, or is there only the one Generator class which extends Generator?

    3. Why do you have GeneratorBase{n} to begin with? Can the Generator class be used directly with no GeneratorBase{n} class?

    4. What is the background for why this structure was adopted in the code, specifically how does other code use these classes?

    5. Why is manually changing which class the Generator class extends inconvenient/undesirable?

    Depending on the answer to each of these questions, a preferable solution changes.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by helloworld922 View Post
    A few important questions to ask (quite similar to each other, just slightly different):

    1. What is the lifetime of GeneratorBase{n}? If you've gone to using GeneratorBase3, are you going to keep and use GeneratorBase2 at the same time in your program? What about GeneratorBase1?

    2. Are there any other classes which need to have similar changes made, or is there only the one Generator class which extends Generator?

    3. Why do you have GeneratorBase{n} to begin with? Can the Generator class be used directly with no GeneratorBase{n} class?

    4. What is the background for why this structure was adopted in the code, specifically how does other code use these classes?

    5. Why is manually changing which class the Generator class extends inconvenient/undesirable?

    Depending on the answer to each of these questions, a preferable solution changes.

    1. i'm not using the all base classes at the same time,.as my version changes my need also changes,..GeneratorBase1,GeneratorBase2 is no longer to Use.
    if my requirement is comes[i.e GeneratorBase1{} ],. then ill compile my generator class and GeneratorBase1, in this condition my generator class automatically extends GeneratorBase1{}, if my requirements tells me that i should use GeneratorBase2{}.then ill compile my generator class and GeneratorBase2,this condition my generator class automatically extends GeneratorBase2{}.

    2.Only one generator class extends generator. In my entire project only two files,.my generator class & my base class.[GeneratorBase1{},GeneratorBase2{}...& So on].When new version of base class comes into picture,.ill remove my old base class from project & keep new base class file[GeneratorBase3{}...]. This time i just compile the code,..it automatically extend GeneratorBase3{}.

    3. No i can't use generator class w/o GeneratorBase class{}

    4. My generator class is have tons of lines of code.which do some unique work. i don want that it should be change. but as my base classes are changing, i have to change my code. So i don wan't do that,.
    What should do,.such that ill just compile my generator class & GeneratorBase{n}, it automatically extend it

    5. If my new base class comes into picture, i'm changing code
    Example : if my Previous Code like this
    Class generator extends GeneratorBase1{}

    if my requirement changes to extend new GeneratorBase2{}, i need to modify as
    Class generator extends GeneratorBase2{}....

    Please suggest me the right way

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    Sounds like a project in dire need of source control software (a.k.a. revision control software), and possibly some re-structuring.

    You really shouldn't be tracking the version of a class in the class itself unless you intend to use multiple versions of a class at the same time. Even doing so, there's usually a better way to name the classes so you don't end up with something like your situation. For example the name of GeneratorBase2 vs. GeneratorBase1 doesn't tell you anything useful, where ElectricGeneratorBase and GasGeneratorBase are better.

    Why do I suggest source control? Because this isn't the case in your application. Once you've decided to use GeneratorBase2, GeneratorBase1 for all intensive purposes is only around for support and backwards-compatibility (maybe sentimental value, too). Clearly, you could just always call the class GeneratorBase, and as you modify the class leave the name GeneratorBase.

    Source control software allows you to track changes made to a file over time, and if you realize tomorrow that you don't like the changes made a few seconds ago, few hours ago, months, or even years ago, you can jump to any previously saved snapshot of where the code was. You can even decide to jump forward if you decide later that you did like a change you reverted.

    Good source control even let you branch your code. This allows you to develop two code paths with some common base in the past, and you can join the two back together in the future, take parts from one and put it into the other, or decide you don't like one and just stop working on that branch all-together.

    I won't discuss all the benefits of source control, there are so many benefits that I use source control for nearly all the code I write. My current favorite flavors of source control right now are Git and SVN. Hg (Mercurial) is also a good flavor, though I tend to prefer Git over Hg. These are all open source solutions, and in my opinion far surpass commercial solutions for anything other than specialized applications.

    No i can't use generator class w/o GeneratorBase class{}
    What I meant is if you took everything in GeneratorBase and put it into the Generator class, would your program still work, or can you modify your other code to accommodate this change fairly easily?

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Hi helloworld92,
    I had been asked this question in Job Interview, I don know what is their Project Design.
    They Just asked this is the design & how you resolve this.
    And I'm really interested know the answer.
    I don know what will be the answer, i Just want to know Is this scenario is possible in java?? if yes, they How ?

  12. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    From a pure Java perspective, I would not do this even if it were possible. There might be some hack using reflection and code generation/compilation during runtime, but these solutions are all quite messy and not something I would ever put into a product.

    Personally I believe the best solution should involve source control software in combination with a program re-design. Without knowing the specifics, I would say the GeneratorBase class shouldn't exist and its contents should be inside the Generator class. Alternatively, if there are multiple GeneratorBase's possible, the base should be common and relatively unchanging (i.e. Generator should be higher up in the inheritance tree). Code you're expecting to branch and be used in multiple contexts should be lower down on the inheritance tree, and the common code which describes the interface should be higher up.

    There may be other reasons you or they didn't mention which would change my answer, but as stated with the given information this is what I would respond with, and if this is their company's practice, I would strongly reconsider if that's somewhere I would want to work.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

    Sounds like a place for an interface. Have all the GeneratorBaseClass classes implement the interface and then pass the selected one to Generator.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  2. Base Changing - Is there an easier method?
    By Staticity in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 2nd, 2011, 05:25 PM
  3. Issue with Base Changing..
    By Staticity in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2011, 12:01 PM
  4. Doubts Over Base Class
    By rakesh86shankar in forum Object Oriented Programming
    Replies: 1
    Last Post: September 29th, 2010, 09:04 AM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM

Tags for this Thread