When you inherit from class it means that derived class is a class that inherits. It means that it has exact the same base functionality that can be expanded, overriden and so on. If it makes no sense that derived class had method that is defined in parent class means that derived class is not a class that it derives.
For real world example let's say you have class FlyingMachine
that has method Fly
and Move
, it would not be logical if you derived class Car
from FlyingMachine
because cars don't fly, while deriving class AirPlane
from mentioned base class is perfectly fine. But it would be logical if you had base class Machine
that had method Move
and car was derived from this class Car : Machine
(car is a machine, right?), FlyingMachine
is a machine too so FlyingMachine : Machine
is perfectly fine and since air-plane is a flying machine AirPlane : FlyingMachine
makes sense.
For your given example this should be something like:
abstract class VeryBaseClass {
public function MethodThatIsLogicalForEveryDerivedClass() {}
}
abstract class BaseClass extends VeryBaseClass {
public function SomeOtherFunctionThatMakesSenseOnlyForSomeClasses() {}
}
class ClassThatShouldNotHaveSpecificFunctionDefinedInBaseClass extends VeryBaseClass{}
class ClassThatShouldHaveSomeOtherMethod extends BaseClass {}
Edit:
If there's a functionality that several derived classes need but for some of them it should be public and for some of them not. You should define in base class this method as protected (derived class can access one but from outside impossible). And in derived class that require that this method was public make another method and call parent's method. E.g.:
abstract class Car { protected function Shift() {} }
class ManualCar extends Car {
public function Shift() { parent::Shift(); }
}
class AutomaticCar extends Car {
//Some implementation of automatic car that uses protected Shift method
}
Little demo