Your example is not so good.
Let's try something different: Vehicles. You can assume that all Vehicles have an engine with a certain power:
public class Vehicle{
private int iPower = 0;
public Vehicle(int iPower){
this.iPower = iPower;
}
}
Now, there are different Vehicles on the road. e.g. some trucks, some cars and some tractors. All those Vehicles have an engine - but some might have additional features:
public class Truck extends Vehicle{
private int iLoad = 0;
public Truck(int iPower){
super(iPower);
}
public Truck (int iPower, int iLoad){
this(iPower);
this.iLoad = iLoad;
}
}
public class Car extends Vehicle{
private int iSeats = 1;
public Car(int iPower){
super(iPower);
}
public Car(int iPower, int iSeats){
this(iPower);
this.iSeats = iSeats;
}
}
We have Vehicles as a base class, Trucks and Cars as more specified Vehicles.
Now here is the trick:
We will set up a Road and let Vehicles - no matter what kind of Vehicles those are - drive on it:
public class Road{
public void drive(List<vehicles> oVehicles){
}
}</vehicles>
So the base class is a general approach to work with "the group" of those objects. We can cast the Vehicle to the more specified version if needed:
Truck oTruck = (Truck)oVehicles.get(0);
But that is just needed when we want to do special things. In general these are Vehicles - that's enough to let them use the road.
This system can be used in a lot of places:
A natural person e.g. is defined by gender, name and date of birth(or) place of birth.
A customer is a natural with a history of bought items.
An employee is also a natural person having other properties (right to enter the depot, can take cash).
A robber is a natural person with a gun.
But all of those natural persons are walking through the shop. Until...