This content originally appeared on DEV Community and was authored by Md Yousuf Gazi
Inheritance is a mechanism or process where one class inherit the properties and functionalities from another class. Just like a child inherit characteristics from his parents. The class that inherit the properties and functionalities of another class is known as child class/subclass. On the other hand, the class whose properties are inherited is known as parent class/superclass.
Why do we need Inheritance?
There are several reasons why we need inheritance.
Reusability: With inheritance, we can use the fields and methods again and again, or we can reuse the whole class wherever we need. As a result, we don't have to write the same code which saves us a lot of time. For example, in the collection framework of java -
Sorted Set inherit Set, Navigable set inherit Sorted set, Tree set inherits Navigable set. Here if we use the Tree set we can use all the functionalities of Set, Sorted set and Navigable set, we don't have to write code for each of them again. That's the privilege we get from inheritance.
Maintenance: As we reuse the code, for safety the reusable code is tested more often, which means the program will have fewer errors means a better program.
Overriding: When we declare the same method in child class which is already present in the parent class with different implementation then this is called method overriding. When we use child class it inherits some functionalities from the parent class, if we need to change a certain functionality because of the requirement we can do it by overriding in the child class and it will execute the method of the child class, not the parent class.
This content originally appeared on DEV Community and was authored by Md Yousuf Gazi
Md Yousuf Gazi | Sciencx (2021-05-01T19:29:25+00:00) Inheritance in Java. Retrieved from https://www.scien.cx/2021/05/01/inheritance-in-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.