Skip to main content

Inheritance in OOP: Introduction

inheritance in oop-image

In the previous section, we talked about what Object-Oriented Programming is and what are its key concepts or features. We defined them in a very brief and formal way. As I promised that we will discuss each of those in details, here it is. I will try to be as natural as possible.
Inheritance is that feature of Object-Oriented programming which allows developers to borrow code from a prototype (class) and extend it, by adding more properties to it and behaviors to manipulate those properties. It reduces the size of the code with a big margin by creating is-a relationships between objects. This line might sound confusing but don’t worry. You will see in the coming section that how it reduces size of the code. The more common properties and behaviors a program have, the more usable inheritance will be. It allows you (the developer) to implement the real world parent-child hierarchy in a computer in a robust way. Here, I will tell you all I know about inheritance, how to implement it in Java and then where to use it.
Above, I said that inheritance allows developers to implement the parent-child hierarchy in computers like the real world. Inheritance is actually based on the parent-child concept. It has the terms ‘parent’ and ‘child’, which we will define each in the context of OOP but first let me ask you a question. Who is a parent and who is a child, in the real world?  I am sure you will have an answer but if you don’t have, I am happy then that my posts are being read far from the earth. Alright, let’s take our topic seriously.
A child (human) inherits traits from their biological parents like their physical appearance. Children also have access to the property of their parents and have the right to use it unless the property is private to their parents. For example, let’s create a scenario, an inheritance story. John is (a) son of Bob. He resembles his father in many ways because he is his son. Gregor Mendel has explained this through his work on pea plants. Anyhow, He (John) lives with his dad and often drives his car, use his shoe polish, towel etc. But John does not have access to his dad’s private mailbox or Facebook account or anything else which is private to his dad. John also goes to school and gets assignments from his teacher. His dad loves him and helps him often times to complete the assignments but he does not take him with himself to the parties at bar cuz those are private (not for kids sort of party) and John is only 13.
Note the boldface is-a relationship and the things John have access to or what his father does and does not for him. You might have noticed that John can use or know about only the non-private things that his dad own or does. Why? Because they have a relationship between which have its own restrictions. Nobody else can use those property (i.e. those are protected). Now, let’s see these terminologies or concepts or whatever you want to call it, in Object-Oriented Programming.
If you are here, it means that you know what a class is and what an object is. In case you don’t, go here.
In inheritance, classes have is-a relation. The class, which inherits from other class(es) is called child class aka subclass, derived class or extended class and the class which the child inherits from is called parent class or super class or base class. We will use child-parent notation throughout this blog. Like the real world, a class can have many child classes and a child class can have many parent classes. You will see a single example for each case in the next section. The parent class can have private, protected and public members and so can have the child class. The child or derived class have access only to non-private (protected and public) members of its parent class(es). Different languages support different types of inheritance. For example, Java supports single and multi-level inheritance while C++ supports single, multiple and multi-level inheritance. Each language has its own syntax and keywords but the basic concepts are the same. You will need to master only one and then you will be able to go with anyone with a very little work.

Types of Inheritance:

inheritance types-infographic
You might be familiar with the words single, multiple and multi-level inheritance. If you are not, no worries. These are different types of inheritance which you are going to learn in this section. Many authors/programmers include more other types such as hierarchical inheritance, multi-path inheritance and hybrid inheritance in their introductory words to the types but I am not including any other than the basic three types here. In my opinion, the other types are formed by combining the basic types (single, multiple and multi-level). Learn the basics well, it will be easy to learn the rest.
Do you remember John, the main character of our inheritance story? Let’s add more people to his family.
John have a caring mom, Sarah and a loving grandpa, Stephen. We do not need to talk about what they can do or what they have for now. To depict my point clearly, I am including an infographic.
Notice that:

  • The child class in Single Inheritance has a single parent.
  • The child class in Multiple Inheritance has more than one parents (at the same level).
  • The child class in Multi-level Inheritance has a parent and the parent class itself inherits from another parent class. 

Bullets, above are not standards. Variation are possible such as in single inheritance a parent class can have many child classes and a child class can have multiple parent classes at the same level in multi-level inheritance. I hope it makes sense to you.
Alright, we are done for today. You are doing great. Keep learning, sky is the limit. There are great content out there which discuss inheritance further in details. I will encourage you to go for it and study every detail until you feel comfortable with it while coding. You can find some helpful stuff here.
Alright, we are done for today. You are doing great. Keep learning, the sky is the limit.
Next is a How To tutorial where we will talk about how to achieve inheritance in Java, its types and what keywords to use, followed by a very important section where we will discuss where to use which type of inheritance in real projects. Subscribe using the subscribe button up in the toolbar and we will notify you when the tea is ready. If you have any questions or suggestions, please leave in the comments below and also mark one of the options in the survey popup.
Happy coding.

Comments

Popular posts from this blog

Introduction to Data Science: What is Big Data?

What Is Big Data First, we will discuss how big data is evaluated step by step process. Evolution of Data How the data evolved and how the big data came. Nowadays the data have been evaluated from different sources like the evolution of technology, IoT(Internet of Things), Social media like Facebook, Instagram, Twitter, YouTube, many other sources the data has been created day by day. 1. Evolution of  Technology We will see how technology is evolved as we see from the below image at the earlier stages we have the landline phone but now we have smartphones of Android, IoS, and HongMeng Os (Huawei)  that are making our life smarter as well as our phone smarter. Apart from that, we have heavily built a desktop for processing of Mb's data that we were using a floppy you will remember how much data it can be stored after that hard disk has been introduced which can stored data in Tb. Now due to modern technology, we can be stored data in the cloud as well. Similarly, nowadays we noticed …

How Big Data Analytics Can Help You Improve And Grow Your Business?

Big Data Analytics There are certain problems that can only solve through big data. Here we discuss the field big data as "Big Data Analytics". The big data came into the picture we never thought how commodity hardware is used to store and manage the data which is reliable and feasible as compared to the costly sources. Now let us discuss a few examples of how big data analytics is useful nowadays. When you go to websites like Amazon, Youtube, Netflix, and any other websites actually they will provide some field in which recommend some product, videos, movies, and some songs for you. What do you think about how they do it? Basically what kind of data they generated on these kind websites. They make sure to analyze properly. The data generated is not small it is actually big data. Now they analysis these big data they make sure whatever you like and whatever you are the preferences accordingly they generate recommendations for you. If you go to Youtube you have noticed it kn…

AI Vs Machine Learning Vs Deep Learning

AI Vs Machine Learning Vs Deep Learning Artificial intelligence, deep learning and machine learning are often confused with each other. These terms are used interchangeably but do they do not refer to the same thing. These terms are closely related to each other which makes it difficult for beginners to spot differences among them. The reason I think of this puzzle is that AI is classified in many ways. It is divided into subfields with respect to the tasks AI is used for such as computer vision, natural language processing, forecasting and prediction, with respect to the type of approach used for learning and the type of data used. Subfields of Artificial Intelligence have much in common which makes it difficult for beginners to clearly differentiate among these areas. Different approaches of AI can process similar data to perform similar tasks. For example Deep learning and SVM both could be used for object detection task. Both have pros and cons. In some cases Machine Learning is …