+ 2

Why use inheritance when you can put everything you need into a class?

I am learning more about Java and would like some real-world advice, not just textbook related answers.

27th Mar 2017, 12:17 AM
Ethan Simons
Ethan Simons - avatar
11 Antworten
+ 13
Inheritance is used to reduce repetition of code. Let's say you were creating multiple Animal objects. For example each animal would have common characteristics such as the ability to move. Instead of creating classes with the move attribute defined in every class, you would simply create a class with unique attributes and methods and inherit the main class which would be Animal, so that you can access the common attributes without having to declare them in all of the subclasses. Hope that helps.
27th Mar 2017, 12:44 AM
Hassie
Hassie - avatar
+ 3
A really good programming example are javas file and collection classes. For example, a bufferedReader accepts every kind of Reader as argument, e.g InputStreamReader or FileReader or your own reader class. The advantage is there is only one class that implements the buffering whereas there are alot of input implementations: from a server, a file or system.in every thing that implements InputStream or Reader can be turned into a BufferedReader. Same for input/outputstreams/writers
27th Mar 2017, 5:49 PM
No One
No One - avatar
+ 2
Yes, that's correct. Then simply the tiger class will extend Animal so that it can inherit the attributes and methods defined in the animal class.
27th Mar 2017, 12:50 AM
Hassie
Hassie - avatar
+ 1
So in other words, the main features like breathing, eating, etc are in the animal class, where the tiger class adds stripes, roaring, etc in addition to what the animal class has?
27th Mar 2017, 12:47 AM
Ethan Simons
Ethan Simons - avatar
+ 1
You're welcome.
27th Mar 2017, 12:55 AM
Hassie
Hassie - avatar
+ 1
inheritance striking feature is reusability
28th Mar 2017, 9:47 AM
nishu
+ 1
inheritance mean in a simple way object of one class acquires the properties of another class.. for example class a extends b mean subclass a will acquire the properties of super class b using keyword extends..
28th Mar 2017, 9:51 AM
nishu
+ 1
here one of the main advantages of oop comes, namely inheritance is useful for reuse of code. once the code is written, but recalled many times as you want, than putting repeated copies of the same code. inheritance improves your codding time, storage , simplifies error debugging and others.
28th Mar 2017, 2:19 PM
WONDE-TOM
WONDE-TOM - avatar
+ 1
i do not think animals are such a good example. everyone makes this example, but in the real world you do not actually program animals in gui development inheritance is very common. for example in a gui framework there exists a window class, which defines the basic behaviour of a window. if you then want to prpgram a custom window, maybe add some buttons, change the begaviour when it is closed, you do not need to write everything on your own. you can inherite from the window class and override methods, thus redefining the basic behaviour with the custom behaviour. as well you could implement multiple different window subclasses but treat them all like the superclass, by putting them in a list that can contain windows. when you call the functiom of a window in the list, the implrmrntstiom of thr specific subclass is used. this is called dynamic binding. and treating different specific implementaions of the same superclass in a general way is called polymorphism
28th Mar 2017, 3:50 PM
Jakob Robert
Jakob Robert - avatar
+ 1
inheritance is to organize the code more so if you're in a company anyone else can understand it... //comments do the same thing
28th Mar 2017, 5:34 PM
Karam Boubou
Karam Boubou - avatar
0
@Hassie thank you!
27th Mar 2017, 12:51 AM
Ethan Simons
Ethan Simons - avatar