First off I am new to java and am teaching my self.
my file structure is:
- ParentClass.java
- ChildClass1.java
- ChildClass2.java
- MyChildClasses.java
both ChildClass1 and 2 extend the ParentClass.
and MyChildClasses has a my main method that works with the child classes
In MyChildClasses main looks like this
ChildClass1[] myClass1 = new ChildClass1[5]
ChildClass2[] myClass2 = new ChildClass2[5]
for(int i = 0; i > 5; i++)
{
//Setting the ClassID #'s
myClass1[i] = new ChildClass1((100+i))
myClass2[i] = new ChildClass2((100+i), (i+1))
}
I am wanting to see if there is a way to make this into 1 array that can have both child classes in it?…
making it much easier to print out on screen.
You can declare the array as
ParentClass[]and assign instances ofChildClass1andChildClass2into it.You’d be better served with a java.util.List though: