When you use the same method name in parent and child classes, you create confusion.
A private method in the parent class cannot be overridden even if a public method with the same name exists in the child class.
This is a problem most static languages have in their design
This disconnect leads to bugs and makes your code hard to maintain.
Sample Code
Wrong
<?classParentClass{privatefunctiongreet(){// This method is privatereturn"Hello from ParentClass";}publicfunctioncallGreet(){return$this->greet();}}classChildClassextendsParentClass{publicfunctiongreet(){// Overriding a concrete method is a code smell// Compilers SHOULD warn youreturn"Hello from ChildClass";}}$child=newChildClass();echo$child->callGreet();// When callGreet() is invoked on the $child object,// it executes the following:// It calls $this->greet(), // which refers to the greet() method of ParentClass // because the original method is private // and cannot be overridden or accessed from ChildClass.// The unexpected output is 'Hello from ParentClass'
Right
<?classParentClass{protectedfunctiongreet(){// notice the 'protected qualifier'return"Hello from ParentClass";}publicfunctioncallGreet(){return$this->greet();}}classChildClassextendsParentClass{publicfunctiongreet(){return"Hello from ChildClass";}}$child=newChildClass();echo$child->callGreet();// The output is "Hello from ChildClass"// This is the standard (and wrong) solution// Also fixed by most AIs
<?abstractclassParentClass{// Declare greet() as an abstract method// Following the template-method design patternabstractprotectedfunctiongreet();publicfunctioncallGreet(){return$this->greet();}}classChildClassextendsParentClass{protectedfunctiongreet(){return"Hello from ChildClass";}}classOtherChildextendsParentClass{protectedfunctiongreet(){return"Hello from OtherChild";}}$child=newChildClass();echo$child->callGreet();// Output: Hello from ChildClass$otherChild=newOtherChild();echo$otherChild->callGreet();// Output: Hello from OtherChild
Detection
[X] Semi-Automatic
You can detect this smell by looking for private methods in parent classes and checking if child classes define methods with the same name.
You must also test parent methods calling private methods.
Tags
Hierarchy
Level
[X] Intermediate
Why the Bijection Is Important
Clear and predictable code should reflect the real-world hierarchy it models.
When you use private methods with overlapping names, you create a Bijection gap between the model and the implementation.
This gap confuses developers, increases defects, and violates clean code principles.
AI Generation
AI generators often create this smell when they generate boilerplate parent-child relationships.
They might not check access levels or consider inheritance implications.
AI Detection
AI tools can fix this smell with clear instructions.
You can ask the AI to check for overlapping method names and refactor hierarchies.
Top comments (0)