Before we go more deeply into explaining the concept of Inheritance, for those new to OOP, let's define the concept of object itself. In the context of OO software, an object can be almost any item or concept, either a physical object (i.e. customers, users, and so forth), or conceptual objects that only exist within software, such as a text input field or a radio button.
Object-oriented software is designed and built as a set of self-contained objects, with both attributes and operations (functions) that interact to make applications work properly. In OOP parlance, attributes are Properties or variables related to the object. Operations or functions are Methods that belong to the object to perform actions for modifying itself or for some other external effect.
One of the major pillars of OOP is Encapsulation (also known as data hiding). In simple terms, access to the data within an object must be available via the object's methods, or the interface of the object. Modifiers and accessor methods should provide a convenient way to access and modify objects properties.
Last, but not least, we need to explain Inheritance. We can create a hierarchical relationship between classes and derived subclasses. A subclass inherits properties and methods from its super class, a fact we can use to save ourselves some work. If we write a super class that owns properties and methods, and then create subclasses that use the capabilities of the super class, we write it only once, and this is definitely better than writing those methods and properties on each subclass (many times, in other words).
If a sentence about two classes makes sense with "is a" between the classes, from bottom to top, inheritance can probably be implemented appropriately. Consider that if we have an input text element, it might be thought of as "is a form element." So, if we would have a form element class, then the input text element can inherit from the form element.
For clarifying the concept of Inheritance, let's show a simple and quick example, since the topic has been thoroughly covered in many books and articles. First, let's define a base class "basicMessage" for, not surprisingly, displaying messages:
Now, let's instantiate an object from the base class:
The output for the above code is the following:
That's very simple and straightforward, right? Now, in order to implement Inheritance, let's define a subclass "boldedMessage," which inherits the properties and methods from "basicMessage:"
Please notice that inside the "boldedMessage" constructor, we've called the constructor for "basicMessage," with the following syntax:
Now, if we instantiate an object from the subclass "boldedMessage," in the following manner:
The visual output is as follows:
Certainly, the subclass is happily inheriting the $message property and the "displayMessage()" method from the base class. Also, for proper implementation, the base class definition must be available, for deriving any number of subclasses. Please don't forget that!
If we ever change our mind, and decide to add the own "displayMessage()" method to the subclass "boldedMessage," overriding the original base class method, the class definition would look like this:
By this point, the subclass is overriding the "displayMessage()," exposing its own method definition, in this case displaying a bolded message. Just by instantiating a new object from the redefined class, in a similar way to the previous examples:
the output would be a bolded string message:
As you can see, any number of derived subclasses from the base class will inherit its properties and methods. Indeed, this is extremely useful in OOP. Just think about it. If we have a super class containing the core properties and methods, and for instance, we need to add another method to it, the new method will be automatically inherited from all of the defined subclasses. If you think that's not enough power, well you should run for president!
Now let's take a more practical approach and use OOP in PHP to build something useful for Web applications. Did you read this article's title? Okay, we're going to develop an extensible form generator, using the goodies of Inheritance and Polymorphism. Keep reading.
blog comments powered by Disqus