I have the below code.
public class Test {
public static void main(String args[])
{
int i = 0;
if(i==0){
Beer obj = new Beer();
}
else {
Rum obj = new Rum();
}
System.out.println(obj.brand); //doesn't work
} }
class Drink {
}
class Beer extends Drink{
public String brand = "BeerBrand"; }
class Rum extends Drink{
public String brand = "RumBrand"; }
- Is there an way to make the above work without using function overriding or dynamic class loading?
- All classes are dynamically loaded in JVM there is no static loading like in C. Is this correct?
The only alternative is to use reflections, but fixing the design of the classes would be much simpler/better
Yes. They can be dynamically loaded more than once and even unloaded.
Using an object orientated approach would look like this.