Object Oriented Programming Inheritance. Is there even a point to it? Every time I see people try to explain it they always use that fucking "Animal", "Dog", "Cat" example. I'm honestly not seeing the benefits here. Can someone give me a practical example?
Name:
Anonymous2011-08-19 23:32
Part of it is to do with sharing code. In C# for example, you have System.IO.StreamReader for streaming input from files and System.IO.StreamWriter for streaming output to files (they aren't the only classes for file IO but bear with me). If you wanted to combine the two, to make a StreamJesus class you would use inheritance. Unfortunately in C# there's no (direct) way to inherit two classes*, but you get the point.
Inheritance is the most overused feature of OO, most C++/Java/C# books teach inheritance as a way to organize code as though you should use it at every possible chance. But the fact is that inheritance is very confining and can bring about a needless explosion of subclassing. This is why Design Patterns were developed, so programmers can unlearn inheritance and use nodal style of OO where classes are interconnected nodes.
If you have read SICP, object inheritance is partially discussed at the end of chapter 2- although it's not explicitly called inheritance.
We wish to have a generic arithmetic package, so we can not only add or subtract with different types (polynomials, rationals, integers..) but compose them, for example to get polynomials with rational coefficients.
This is achieved almost effortlessly through use of data directed programming, and what we are really doing in OO terms, is constructing an inheritance tree; where each of the primitive types (polynomials, rationals, etc..) implements a base generic type (not explicitly named in SICP, although it's analogous to the apply-generic function).
I won't explain it further because you can go and read SICP yourself- if not you should probably reconsider your career.
Inheritance is great for things like GUIs. If you ever written a desktop application, the benefit will be obvious.
Name:
Anonymous2011-08-20 18:38
I wrote a program which intercepted packets in real time, and since all packets share common traits, the classes for incoming and outgoing packets were derived from a common packet class.
Name:
Anonymous2011-08-20 18:41
>>15
I forgot to mention: it was only useful because I could store all packets in one container.
Name:
Anonymous2011-08-20 19:04
inheritance is used in C++ for code sharing and static interface assurance, which actually are two very different things that shouldn't be confused.
Name:
Anonymous2011-08-21 1:53
>>14
That's funny, because I've found that the dark side of inheritance is most obvious when trying to write a GUI. Teetering your way through some kind of stubbornly linear widget phylum is not just aggravating, but a blatant case of hammering a screw.