I need to instantiate a class during runtime based what a user has selected in a list, but these classes each have their own new specific functions and variables, they use the same named functions, but set different variables, and different numbers of variables.

I made an example, since my actual code has classes i made which you'd need to import

class tester {

public void tester(){};

 public void main(){
int x=1;
if (x==0){
class1 newobj = new class1();
}
else if (x==1){
class2 newobj = new class2();
}

newobj.testfunc();

}

        private class class1{
        public void testfunc(){
        System.out.println("class1");
        }
        }
        private class class2{
        public void testfunc(){
        System.out.println("class2");
        }
        }

this gives me a variable newobj not found error

so I made a general superclass just to declare a variable at the start, and then cast it and use their functions after the choice check

class tester {
public void tester(){};
public void main(){
int x=1;
Object newobj;
if (x==0){
newobj = new class1();
newobj = (class1)newobj;
}
else if (x==1){
newobj = new class2();
newobj = (class2)newobj();
}
newobj.testfunc();
}
            private class class1 extends Object{
            public void testfun(){
            System.out.println("class1");
            }
            }
            private class class2 extends Object{
            public void testfunc(){
            System.out.println("class2");
            }
            }
}

which gave me a method not found error...

so I made a superclass with the method

class tester {
public void tester(){};
public void main(String[] args){
int x=1;
superclass newobj;
if (x==0){
newobj = new class1();
newobj = (class1)newobj;
}
else {
newobj = new class2();
newobj = (class2)newobj;
}
newobj.testfunc();
}
 

        private class superclass{
        public void testfunc(){
        System.out.println("superclass");
        }
        }
        private class class1 extends superclass{
        int specialvariable;
        public void testfunc(){
        System.out.println("class1");
        specialvariable = 100
        }
        }
        private class class2 extends superclass{
        int specialvariable2;
        public void testfunc(){
        System.out.println("class2");
        specialvariable2 = 100
        }
        }
}

This just runs the method of the superclass

I don't understand why this last method doesn't work...

Even though I use the subclass' constructor to initialize it and cast it as well, how come I don't get an object of that specific subclass?

Some other info:

The user will have an item in a JList selected, then clicking on a panel will create a specific label subclass I defined in the package, the classes aren't inner classes like here, but I get the same problems. And after being created they all call the same method (by same I mean same name, different actions/variables) outside the if statements, so no matter what they picked I can just write that newobj.method() once, like in that example

Edited 5 Years Ago by Srin: n/a

I looked at your final version. Of course it won't compile, so can't be the code you actually ran. (That doesn't help diagnose you know, when the code you post isn't the code you had a problem with).
Anyway, I fixed the missing semicolons and the static method references to non-static inner classes, and it works perfectly.
ps the casts are redundant.

Sorry, like I said my actual code needed imports from my classes so I tried to make something as close as possible as an example

and I i just used casts cause I wanted to try everything

anddd

Anyway, I fixed the missing semicolons and the static method references to non-static inner classes, and it works perfectly.

sorry I'm new at this, what exactly does that mean? what was the static method reference problem?

The way you posted the code the classes were instance inner classes, so you couldn't instantiate them from static main without an instance of the outer class.
Anyway, this is the cleaned up version that worked for me:

public class Demo {

   public static void main(String[] args) {
      new Demo();
   }

   Demo() {
      superclass newobj;
      for (int x = 0; x <= 1; x++) {
         if (x == 0) {
            newobj = new class1();
            // newobj = (class1) newobj;
         } else {
            newobj = new class2();
            // 
            newobj = (class2) newobj;
         }
         newobj.testfunc();
      }
   }

   private class superclass {

      public void testfunc() {
         System.out.println("superclass");
      }
   }

   private class class1 extends superclass {

      int specialvariable;

      public void testfunc() {
         System.out.println("class1");
         specialvariable = 100;
      }
   }

   private class class2 extends superclass {

      int specialvariable2;

      public void testfunc() {
         System.out.println("class2");
         specialvariable2 = 100;
      }
   }
}

Main thing is that your final try was exactly on the right lines. I don't know why your actual code wasn't working

ps If your actual app already has those classes extending something else, you can define an interface with the common method name(s) and have them all implement that.

Edited 5 Years Ago by JamesCherrill: ps

Oh ok, this is my first actual program, I just spent the last 3 weeks reading java, didn't have any real writing experience, so I have lots of little typos/syntax errors

And basically, I have a general superclass that extends a label and has method bodies. Then that class has about 10 subclasses, the majority of which use the same method definition as the general superclass but some have to override it and do something different. I thought about using an interface, but then I'd have to copy and paste the method bodies between all classes that use the same method and didn't want the redundancy.

And running your demo worked for me, so I don't understand why my other code doesn't, maybe I just forgot to add something here or there again.

Btw, how many generations can you downcast and still use all the available methods of that specific subclass?

Like, can I make it
topclass newobj;
and use something very specific class that has been extended many many times from that topclass? Or is the restriction just that the topclass also has to have that method (in this casetestfunc) declared, even if empty?

Edited 5 Years Ago by Srin: n/a

Short answer: You declare
Class49 aVar;
now var49 will be constrained only to refer to instances of Class49 or subclasses (no matter how deeply nested/subclassed) of that class.
When you code
aVar.doSomething();
the compiler checks that .doSomething() is valid for Class49. Since all subclasses (direct or indirect) of Class49 either inherit or override all of Class49's accessible methods, the statement is valid, regardless of the value of aVar.

ps: Beware; different logic applies to static methods

Edited 5 Years Ago by JamesCherrill: ps

Ok but if Class50 is a subclass of Class49, which has a method called doSomething50()
Class49 aVar
if (userchoice = "50"){
aVar = new Class50()
aVar.doSomething50()
}
else if...//other options

that wouldn't compile?

I need to learn more about how java works internally.
Thanks for all the help!

I need to learn more about how java works internally.

Well, we ALL need to learn more. When you stop learning you're dead.

Thanks for all the help!

My pleasure.
J

This question has already been answered. Start a new discussion instead.