I have 3 classes, MainClass with main method, one abstract class named AbstractClass and Subclass which is to be extended with AbstractClass.
The array of objects is created in main method from type AbstractClass containing 1 member. Then I initialize that 1 element of array as type Subclass( is this ok?). The problem is I can't get to the getDataToExport() method of created object ( array[0] ) . I suspect the problem occurs because the array is the AbstractClass type... And the question: is this even possible to accomplish? What I'm trying to do is use an array of type AbstractClass and fill it with objects made from different subclasses( in this code is just one->Subclass ) extended with AbstractClass but i can't get to the methods of that subclasses.
main class with main method
public class MainClass {
public static void main() {
AbstractClass array[]=new AbstractClass[1];
array[0]= new Subclass(); // is this even allowed?
System.out.println(array[0].getDataToExport()); // Problem!
}
}
abstract class
public abstract class AbstractClass {
}
Subclass which extends AbstractClass
public class Subclass extends AbstractClass {
private int dataToExport;
public Subclass(){
this.dataToExport=2;
}
public int getDataToExport() {
return dataToExport;
}
}