My project consists of two classes: type 1 and type 2 is that each has its own functions and fields I'm gonna use them as follows:
private void initialize(String type) {
if (type == "Type1") {
x = new Type1;
} else if (type == "Type2") {
x = new Type2;
}
}
What type of X variable must be ?
<Update 1>=============================
I use superclass and interface but I do not have access to variables and methods of type1 or type2 and only have access to variables and methods of the superclass
<Update 2>=============================
public class Type1 extends SuperClass{
public int var = 1;
}
public class Type2 extends SuperClass{
public int var = 2;
}
private void initialize(String type) {
switch (type) {
case "Type1":
x = new Type1();
break;
case "Type2":
x = new Type2();
break;
}
}
void main(){
//int num = x.var;
}
In this case can not be used to cast : ((Type1)x).var