Today, I am going to cover child classes, inheritance, the extends, and the super keywords. So, without further ado, let’s get this party started.
So, to revise my previous statement, I like to think of a “parent class” as being like Chipotle; people go in, and they come out with meals. What about those meals, though? They may be different from one another, but they are made out of the same ingredients.
In order to create a Parent class, the first thing that we need to consider is what all of the meals all have in common. Whether you are getting a burrito, tacos, a burrito bowl, or a taco salad, they all share the following options: lettuce, beans, protein, veggies, cheese, guac, sour cream, and salsa….right? Since you can make a taco salad without adding to those ingredients, let’s make our parent class a taco salad.
Step 1: Class declaration
Now that we have the TacoSalad class declared, we want to add the constructor method. Remember, the constructor method is like your burrito artist (in this case your meal artist), which means that we need to give it the parameters of all the ingredients to make a taco salad, like so.
Step 2: Add a constructor & methods
Now we can make a salad with any ingredients that we choose, that wasn’t so bad, right? But, what if a customer comes in and they want a burrito bowl? We could create a new class called BurritoBowl, but a burrito bowl and a taco salad seem pretty similar. That would be kind of like having an ingredient station for taco salads, one for burrito bowls, one for tacos, and one for burritos.
That would be pretty redundant right? Instead we could add rice to the ingredient station and add it to the meal only when it’s needed. This would be a good opportunity to use the extends keyword; we can use extends to create a class that is a child class of TacoSalad. The child class will inherit all of the key-value pairs and methods of the parent…kinda like I inherited my dad’s hairline. Let’s take a look at how this works.
With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...
You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...
Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.