To further clarify things, it may help to think it in this way. Let's say your Main
is a class that stands for a vehicle (and hence we rename it Vehicle
), so class means that Vehicle
defines the characteristics of a veicle but not a particular one to do operations with (we need an instance for that).
Let's translate also your class A in Car
and B in Truck
: therefore they are specializations of your main class that, along with possessing every characteristic of a generic Vehicle
, extend the concept of "vehicle" and point out specific behaviours and properties of Cars in general, and Trucks in general, but - and that's the point - don't reference a particular car or truck. We need instances for that.
class Vehicle
{
public $numberOfTires;
}
class Car extends Vehicle
{
function smashMe()
{
echo "Oops!";
}
}
class Truck extends Vehicle
{
function smashMe()
{
echo "More Oops!";
}
}
From this point we can define particular instances (your Porsche, my Camaro... different cars) on which we may perform operations (calling methods) and set properties.
$genericVehicle = new Vehicle;
$myPorsche = new Car;
$yourCamaro = new Car;
$hisTruck = new Truck;
$herTruck = new Truck;
But every instance remains independent from the other.