I am very new to Java and I was wondering if it is possible to assign an object to another object. For example, supposing I have an object called Classroom. In this classroom, I have got chairs, tables, board etc.
Chairs, tables and the board are objects themselves with their own separate attributes such as number of chair legs, table legs, width of the board etc. So basically, I have 4 classes. Now I want to assign the chair, table, board to be attributes of the ClassRoom. I am not sure how to go about this.
Any help would be appreciated,
Thanks in advance
Ester
I’m not a big java guy but I suspect it is similar to doing this in C++…
As long as the classes are defined you should be able to declare them as properties of another object just like you would with something such as a String.
Just make sure each object is defined and it’s supporting methods are defined within that class.
This may not be exactly syntactically correct, but it should look something like this:
Now if you want to access the classRoom chair you just do something like this:
Although note that this is bad practice, you should set up functions to get such values, instead of directly accessing them.