Do you want to learn about Inheritance in a fun way ???
Then don’t worry I’m going to explain it in a very simple way, I will give the Real-World Examples so that you will able relate and understand properly.
Even I explained it in the video too, you can checkout using this link.
When we heard about the Inheritance, some of the few questions arise in our minds.
- What is Inheritance?
- What is the purpose of Inheritance OR Advantages of Inheritance?
- Why is it so much important in any Object-oriented programming?
- What is Real World Implementation?
I will answer all of the questions in further paragraphs, so let’s begin.
What is Inheritance?
It is a process of acquiring the properties of one class into another class then, it is considered to be an Inheritance.
Here properties refer to Methods and Variables present inside the class.
Real-World Examples
Example 1. For now, Consider you want to purchase one book from an online shopping portal so what will be your steps.
- Check out the shopping portal, under that, you might find one category named Product.
- Under the Product, you will see lots of items available for sale.
- Select the item named Book and find the book which you want to purchase.
- Proceed for payment.
Here have you notice two important terms Product and Book. In this case, Book is one kind of Product in that Shopping Portal.
Now one thing is very common here, each item is under the Product category must have the following description.
- Discount
- Price
- Review Rating
Since it is very common and Product is a generic term of all items, so these properties must also be available in the book. Because a book is one kind of product.
Moving further, let us try to analyze this scenario with the Java perspective.
Since in the above example product is a generic term that is used to represents any of the items, that means Book is also one kind of Product.
So, In Java, we put all properties which are common for all other items in one class.
Consider the above example, I will create a class named Product and declared all three variable inside that class. As shown below.
And I will also create one more class named Book. Since the book is one type of Product in that Shopping Portal, so it also has
that three properties such as discount, price, review rating.
But here no need to declare all these three properties inside the Book class.
If need to find some way to achieve all three properties inside a Book Class without writing a single line of code.
Here the concept of inheritance comes into the picture. With the help of inheritance, you can achieve this,
just need to inherit the class Product.
How will you inherit the Product class or any class?
By using the extends keyword you will able to achieve the inheritance. As shown below.
Once you achieve the inheritance, you will easily able to use all the properties such as methods or variables in the other class.
In the above example, we use that variable on the object reference of Book class, as shown below.
Note: Object reference is a variable that stored the address of Object.
Now you can see with the help of object reference of Book class, we are able to initialize the variable of Product class, which means we achieve the inheritance.
Example 2. What are the things do you have which are inherited from your parents??
- You have the thinking ability.
- You can see any physical objects.
- Your body has some height and many more.
These all thing is transmitted from your parents to you. That is only Inheritance.
Important Points About Inheritance
- Inheritance is a process of acquiring the properties from one class to another class.
- Technically we achieve inheritance by using the keyword called extends.
- The class which has the common properties like methods and a variable is also called a Parent or Base or Super Class.
- The class which inherits the Parent Class is called the Child or Derived or the Sub Class.
- Inheritance is also called IS A RELATIONSHIP.
In the above example
- Book: Child or Derived or the Sub Class.
- Product: Parent or Base or Super Class.
Advantages of Inheritance
- Avoid code duplication or redundancy.
- Increase the code reusability.
Thank you for reading this article, if have any suggestion or doubt, please feel free to ask in the comment box.