In biology, every mammal has specific features that ultimately classify it in this large group. But every mammal has its own peculiarities – some fly, others swim, some have four legs and others two legs. Analyzing this context, you will now find it easier to understand how is the concept of inheritance in object-oriented programming.

In OOP, inheritance is done through a superclass that provides its subclasses a number of attributes and methods already ready. For example, the class “doberman” can inherit the superclass “dog” the method bark (), for example. In the same context, the “rottweiler” class can also inherit this same method, but the other attributes as fell and weight will be different.

Let us understand in practice how to work with this concept:

class Dog{

public $weight;

public $fell;

function Bark{

echo “woof woof”;

}

}

class Doberman extends Dog{

$weight = “97lbs”;

$fell = “short”;

}

class Rottweiler extends Dog{

$weight = “121lbs”;

$fell = “medium”;

}

In the block of code shown in the example, the Doberman and Rottweiler classes inherit the attributes and methods of the superclass Dog. The bark () method is the same for both, but the value of the attributes have changed.

You can access public attributes and methods of the superclass only instantiating objects.

$dob = new Doberman;

$rot = new Rottweiler;

$dob->Bark()

$rot->Bark()

Note that there is a great reuse of the generated source code, so that common methods to both classes did not need to be written again. The concept of inheritance is useful precisely at this point.

On a daily basis, the programmer can then create superclasses that will serve as a sort of “template” for subclasses, writing only once a method that can be reused in many others. Remember that a superclass can be used by several subclasses, simply by performing the indication of the superclass using the command extends and, soon after, the name of the superclass.

Source code reuse is indispensable for any productive development environment. Therefore, to understand the concept of heritage and apply it is essential to ensure greater productivity.

Check out more content on our blog!
Learn all about Scriptcase.

By ,

May 18, 2015

a

You might also like…

AI-Driven Software Development: The Role of ChatGPT

In the rapidly evolving world of technology, artificial intelligence (AI) is playing an increasingl...

Low-Code: The Key to Accessible Digital Transformation

Digital transformation is imperative for companies wishing to remain competitive in the current era...

Low-code: Simplifying Development Without Sacrificing Quality

In today's agile and competitive world, productivity is crucial for the success of any business. Th...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.