+ 3

Why inheriting?

Hi, usually you import different classes (like I.e. java.util.* etc.) why do I need to extend super classes or implement other classes then? To make private variables etc available?

22nd Feb 2018, 7:50 AM
Bartinho
4 Answers
+ 2
When you want to add new attributes or new functionalities to an existing class, you extend it.
22nd Feb 2018, 3:53 PM
FabrĂ­cio Lombardi Ribeiro
FabrĂ­cio Lombardi Ribeiro - avatar
+ 2
As Fabricio said, there's a difference between just using an existing class and implementing changes to an existing class, and this is why things like Method Overriding takes place, calling a parent's method you get the parents logic, but calling the child's method you get specific new functionality plus the parent's functionality, extending it.
22nd Feb 2018, 9:53 PM
Roberto Guisarre
Roberto Guisarre - avatar
+ 1
Thank you for your answers. So I basically add stuff to imported classes, right? And to not create a class I just extend that imported class?
8th Mar 2018, 7:32 AM
Bartinho
+ 1
That's right, when you have a functionality, say a method in a class that does almost everything that you need but not all, then you would create your own class, extend the class with that method, and using the same method name, returning the same thing and with the same name, you are overwriting the parent's method, cause you are calling using "super" the parent's method implicitly or explicitly, plus the logic you need for a version that have everything you need. You do need to create your own class to have your extended version of the parent but with your implementations, so that when you instantiate an object with your class you get the parent and your logic. Welcome to inheritance.
8th Mar 2018, 4:38 PM
Roberto Guisarre
Roberto Guisarre - avatar