+ 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?
4 Answers
+ 2
When you want to add new attributes or new functionalities to an existing class, you extend it.
+ 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.
+ 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?
+ 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.