I am really confused by this task. Knowing that instances of an abstract class can be managed by using their subclasses consider this: I was given an UML and it says I have an abstract class Room
that has a constructor with arguments and a concrete method. Room
also includes private fields length
and width
. Class Flat
extends the abstract class, but Flat
has no constructor. Another class called Building
is used to manage all objects of Room
via an array Room allRooms []
.
public abstract class Room {
private float length;
private float width;
public Room(float length, float width) {
this.length = length;
this.width = width;
}
public calcSurface () {
return this.length * this.width;
}
}
public class Flat extends Room {
...
/* no constructor is intended here, not even methods are getting
overriden in here */
...
}
So after writing this I think, except extend Room
there is no real connection between these two classes...
public class Building {
public Room allRooms[];
...
public boolean addRoom (Room room) {
... // for loop
Room allRooms[i] = room; //so here are my passed object. but
since i can't instantiate an abstract class I usually would have
used the (sub)classes ==> = {new Flat(...)};
...
}
Because of the fact Room
contains private fields and is an abstract class, I can't really figure out how to use them without any real connection between my classes.
// method to calculate the surface of all rooms and flats
public float getSumOfRommsurface() {
}
public float getSurfaceOfFlats() {
}
So: How can I access Flat
through Building
and how can I instantiate Flat
if there is no constructor, except the one inside the abstract class.
However, maybe there is a special concept of inheritance and association I have never heard before, if so, I would be really glad if someone could explain it to me in plain English.