Skip to content

Inheritance in OOP

July 7, 2011

Inheritance is a hallmark concept in OO.  Let me try to explain and give examples or how it works.  While the concepts are agnostic, the examples are implemented in PHP. Agnostic meaning the concept is not specific to any one implementation but applies to all implementations.

Through the concept of inheritance, you can encapsulate the common characteristics of a group of related entities into a common parent entity.  *

This ” lifting out ” of the commonality from the ” many entities ” into a single entity greatly reduces duplication of code while adding only a small level of abstraction.

The further up the inheritance tree one goes, the more commonality one should find.  This probably means the classes further up with have a bit more meat to them.  The further down the inheritance tree one goes the more specialty behavior one should find.  Specialty behavior meaning unique to a specific class.







Inheritance means the sharing of properties of a parent with a child.  To find the common attributes of children and define them once in their parent.  Inheritance can greatly reduce the duplication of code across many classes.

When i built my last *package of classes to test my understanding of these concepts, i still was not getting it real well.  I was repeating methods in sub classes that were extending a parent class.  When i started to compile the classes, i saw the error in my design and thinking.  I ended up removing the duplicate methods from the sub-classes and pushed it up once into the parent class.  Then i updated the attributes that contained the data that was unique to the instance and sent that from the sub-classes.  In other words, i was able to define state in the subclass and inhertit behavior from the parent class. That sounds really good, ill say it again.  After careful analysis, i realized the state or data was different in the sub- classes and most of the behavior was the same, which meant push it up to the parent class.

I need to keep working on this, reading about, looking at other examples.  Slowly, i start to really see this as I should.

There is a LMS (learning management system) called Moodle.  It has a concept of a user.  The users of the system can act in different roles.  The roles could be participant, teacher, instructor, advisor, principal. Each of these types of user share common attributes and things that they can do.  Rather than defining those attributes and things they can do in each class, they are defined once in a parent class.  Attributes could be id, email, and school district.  These would all be common attributes that would exist in each of the roles. They could all be assigned a role, enrolled in a class or have their profile updated.  Each of these actions is common to any of the roles.

Each of the attributes and actions would be defined in the parent class user.  Each of the more specific roles (children) would be inheriting the attributes and actions of the parent class. One of the earliest motivations for using inheritance was the reuse of code which already existed in another class.  Why re-invent the wheel?

Another example, sedans, sports cars, and roadsters are all types of cars. In the object oriented language, sports cars, sedans, and roadsters are subclasses of the class cars. The class cars is a “superclass” of sedans, roadsters, and sports cars.  Every subclass will inherit a state from the superclass.

A good example using the common bicycle explanation and visual

Source documents:




From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: