Suppose we design a parking lot class. There are two ways to define the vehicles.
1)
Abstract class Vehicle {
protected int size;
private string plate;
...
}
class car : Vehicle{
car(){base.size = enum.carSize;}
}
class bus: Vehicle{
bus(){base.size = enum.busSize;}
}
2)
class Vehicle {
private int size;
protected string plate;
public setSize(int size);
}
Then in parking lot class we can define
Vehicle Car;
vehicle Bus;
Which one is better? I see 1) is used in "cracking the coding interview". But I see 2) is better as it is simple. What should I use in interview?
If you do not need different kinds of functionalty for different kinds of Vehicle you dont need inheritance.