Inheritance

Definition: allow derived classes to share interfaces and codes of their base classes

By using inheritance, there are two things that we have to be aware of which is the Parent Class and the Child Class. Whatever code such as function or variable that is written on a Parent Class can be used or called by the Child Class replacing the default value. This is another way to illustrate inheritance.

It is crucial to understand what a inheritance is as this is the most common interview questions where you will be asked to explain what a inheritance is.

Let’s try implementing it on the code and understand the concept of inheritance.

The first step to this is to go to File > New > Playground and give it a name

Let’s create out Parent Class. For this, I am making Smartphone as a Parent Class with several different variables.

With the parent class, I added a function in there which will produce the noise of the ring tone. The next step would be adding init() function which will then give us the access to the properties in Smartphones later on. The parent class should be complete by now.

The next thing that we will have to do is to create child class which I have already created an iPhone and a Samsung. You will probably notice that in the child class, we use override init() along with super.init() which allow us to inherit the variables and give it a value.

You can play along with inheritance by implementing more child class. I hope this will boost your understanding in inheritance.

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*