Assign Different Objects to Array
Hello everyone!
I’m new to Java, we just start it in college 3 month ago and I got my 1st assignment
So I have 5 classes
1) “Building” – this is base class (template)
2)3)4) “Office”, “Industrial”, “Residential” – They are inherited from “Building” and have some common attributes and different attributes, the same with methods.
5) Control class – main
The idea is to store classes (2,3,4) in array, access them and modify.
Problem:
If I assign array like this
Code:
Building [] list = new Building [100];
And save it to array like this
Code:
Office office = new Office{***constructor***};
List[pos]=office;
It stores fine and I can display all but I can’t access some methods which are specific to “Office” class because they are not mentioned in “Building”
Ok, it is logical and I understand it
Now I have question:
Is there any way to define some abstract array of objects not depending on the any exist class
Where I would be able to store all my 3 different classes and access they’re methods depending on the class?
P.S. I can define 3 arrays for each class
Code:
Office[] office = new Office[SIZE];
Industrial[] industrial = new Industrial[SIZE];
Residental[] resident = new Residental[SIZE];
but I believe it is stupid and not right. I don’t need 3 different arrays I need just 1
Could you please advise me something? Preferably with simple language/explanation cause I’m not so advanced in Java to understand fancy Java jargon
Thank you very much in advance