I'm trying to solve an inheritance exercise and I found a question I saw that has a chance of appearing elsewhere and so I thought I should ask here. The exercise would ask you to first create a Voo
class that represents an airplane flight and is able to manipulate information about seat occupancy, part of which is that each flight has a maximum of 100 seats.
Then you are asked to create an heir class that allows you to set the maximum number of seats and split the plane into smokers and nonsmokers. With regard to part of smokers I know how to implement. What I do not know is the variable number of chairs because of the way I implemented the base class.
Since there are 100 seats, I've created a 100 bool array in the base class where the i
-this entry is true
if the i
seat is busy. Basically I did this by initializing the array in the constructor as follows:
public class Voo
{
private bool[] ocupacaoAssentos;
public int Numero { get; private set; }
public Data Data { get; private set; }
public Voo(int numeroVoo, Data data)
{
this.Numero = numeroVoo;
this.Data = data;
this.ocupacaoAssentos = new bool[100];
}
}
Then methods manipulate this array. The problem is that the heiress class would not be able to modify this. As far as I know, in C # the heir class constructor always calls the base class constructor.
A possible solution (in C #) would be to implement this with a collection class that has no fixed size and then create a readonly static field that says the maximum of seats and then modify it in the heiress class and from there make checks for that everything is within limits. I do not know if the exercise point would be valid, because the exercise is in Java and I do not know if Java has these collection classes like C #.
But what if I had no way to modify the base class for some reason or some other detail of the problem, I simply could not apply such a solution? How could I deal with this? This is a case that it seems to me that the base class is more general than the inherit class.