6
I’m trying to solve an inheritance exercise and I found a question that I saw that you have a chance to show up in other places and so I thought I should ask here. The exercise asked to first create a class Voo
which represents an aeroplane flight and is capable of handling seat occupancy information, part of which is that each flight has a maximum of 100 seats.
Then it is asked to create an heiress class that allows the definition of the number of maximum chairs and to split the plane into smokers and non-smokers. Regarding the part of smokers I know how to implement. What I don’t know is the variable number of chairs because of the way I implemented the base class.
As there are 100 seats, I created an array of 100 size bool in the base class in which the i
-nth entry is true
if the seat i
is busy. I basically 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];
}
}
So the methods manipulate this array. The problem is that the class heiress would not have been able to modify this. As far as I know, in C# the heiress class constructor always calls the base class constructor.
A possible solution (in C#) would implement this with a collection class that does not have fixed size and then create a static readonly field that says the maximum of seats and then modify it in the heiress class and from there do checks so that everything stays within the limits. I don’t know if the exercise point would be valid, because the exercise is in Java and I don’t know if Java has these collection classes like C#.
But what if I couldn’t modify the base class for some reason or for some other detail of the problem I just couldn’t apply such a solution? How could I handle it? This is a case that seems to me that the base class is more general than the heiress class.
I don’t know if I understand, but the question of the daughter class having to manipulate the array (I don’t know if this is really necessary, I didn’t do this analysis), you could change the array to be
proteceted
, so the daughter would have access. You have to think about it, there are few cases that a protected field is really necessary. In C# it is much more common to use a collection (a list for example) than an array. An array can be considered implementation detail or premature optimization in some cases. The base class has to be more general even.– Maniero
So @bigown, switching to the collection class seemed to me something better done, but the solution of having a static readonly field saying the maximum seems bad too, because it would have to be protected. In this case, I thought I would do it differently: by the number of variable seats in the base class with standard 100 and then use the daughter class only to implement the functionality of smokers. So it would make more sense to inherit?
– SomeDeveloper