Inheritance in Java

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 …

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.


Print Share Comment Cite Upload Translate
APA
Md Yousuf Gazi | Sciencx (2024-03-29T12:35:06+00:00) » Inheritance in Java. Retrieved from https://www.scien.cx/2021/05/01/inheritance-in-java/.
MLA
" » Inheritance in Java." Md Yousuf Gazi | Sciencx - Saturday May 1, 2021, https://www.scien.cx/2021/05/01/inheritance-in-java/
HARVARD
Md Yousuf Gazi | Sciencx Saturday May 1, 2021 » Inheritance in Java., viewed 2024-03-29T12:35:06+00:00,<https://www.scien.cx/2021/05/01/inheritance-in-java/>
VANCOUVER
Md Yousuf Gazi | Sciencx - » Inheritance in Java. [Internet]. [Accessed 2024-03-29T12:35:06+00:00]. Available from: https://www.scien.cx/2021/05/01/inheritance-in-java/
CHICAGO
" » Inheritance in Java." Md Yousuf Gazi | Sciencx - Accessed 2024-03-29T12:35:06+00:00. https://www.scien.cx/2021/05/01/inheritance-in-java/
IEEE
" » Inheritance in Java." Md Yousuf Gazi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/01/inheritance-in-java/. [Accessed: 2024-03-29T12:35:06+00:00]
rf:citation
» Inheritance in Java | Md Yousuf Gazi | Sciencx | https://www.scien.cx/2021/05/01/inheritance-in-java/ | 2024-03-29T12:35:06+00:00
https://github.com/addpipe/simple-recorderjs-demo