?
Solved

Is inheritance evil?

Posted on 2007-11-15
9
Medium Priority
?
381 Views
Last Modified: 2011-10-13
I've just been told by work not to use any inheritance.  I've been trying to do some reading about it and i just can't get my head around it yet.  Allot of the people who post/blog have been burned in the past, but i'm still yet to be burn't.  I guess that's from my lack of large projects.

So is it really that bad?  Any hints/examples/suggestions on what i can do to get my head around this?

I know their is no simple answer, so i'm happy to share the points around =)

Thanks for any help.
0
Comment
Question by:UnexplainedWays
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 5

Accepted Solution

by:
pavaneeshkumar earned 560 total points
ID: 20288868
inheritance is not bad. It serves the purpose of reuse the code that we already developed and speedups the developement.

have a look on this link to find out more

http://www.eskimo.com/~johnnyb/computers/FactoringInheritance/factoringinheritance.html
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 560 total points
ID: 20288899
Inheritance is a great tool. But as with all tools, use with caution.

I've come from Delphi to PHP.

I have a base object class which supports the idea of "ownership". As part of the shutdown phase any object which "owns" another destroys its "children" first.

That way you have a First In Last Out mechanism.

Ownership can be transferred.

If you take something like a window with lots of controls which may have more controls, if you just free the window without freeing the controls, you may well lose access to the controls and you now have a "leak".

Inheritence from my base object solves that problem.


In my mind inheritence solves many problems. Create a generic base class and extend it to become more specialised.

I have this a lot ...

abstract_class_which_would_work_if_it_had_some_config_data implements config_interface

real_class extends abstract_class_which_would_work_if_it_had_some_config_data implements config_interface

Here the real class extends the abstract class but also requires the use of a config_interface to be able to consistently pass data upto the parent (I can use my config_interface between any number of base and special classes knowing that I always have the appropriate methods at hand).

But, one of the major problems I have is multiple inheritence. This is not common in many languages.


If you develop a "deep" tree of inheritence, bubbling functionality from the base to the outer most class can take a while (in cpu terms).

If you move code from 1 level to another, the knock on effect may be unpredictable.

In the past I've used a class flattener, which takes the "deep" objects and constructs a single class with no inheritence. This is then used in the main code.

Sure, you end up with a LOT of duplicated code, but a single class does all the work, so determining which "level" to look at is taken away.

So, create with inheritence but create new classes when you are ready for running which are flat.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 380 total points
ID: 20288901
In my honest opinion, those saying that inheritance is evil, have few skills about Object Oriented programming (vb programmers maybe?), but notice if you are working with pure database applications, then inheritance will not be necessary in most cases.

Inheritance is useful in many cases, but some book authors doesn't contribute to clarify with a real world example, insted they use stupid examples as Cat is derived from Animal or Car is derived from Vehicle.
Multiple inheritance, countersense, could be confusing and an error source, unless you are an experienced programmer. C# doesn't support multiple inheritance, C++ does.
0
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 280 total points
ID: 20289026
Saying inheritance is evil is like saying hammers are evil. They are both tools that can be very useful when used appropriately.

When you are developing an application and you find you are writing the same bit of code (or copying and pasting the same bit of code) over and over again between your classes, you either have a candidate for inheritance or for a utility class.

If the classes are related in some way (you're creating an HR system and you have Employee, Manager, Contractor) and you keep copying your Save method from one class to another, you probably have a candidate for inheritance.

If, on the other hand, you're needing to calculate the day of the week given a particular date (not a good example because the framework already has that) from several disparate classes (e.g. Employee, Order, Paycheck) then inheritance isn't the right solution.
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 220 total points
ID: 20344660
Inheritance, in and of itself is not evil.  However, it may depend upon how the base classes were implemented.  I have encountered situations where performance penalties were encountered because of poorly architecture of base classes.  For example, while working with an early implementation of a C++ String  class, the way in which constructors and destructors were allocating and freeing storage associated with temporary values was causing an enormous (overhead) penalty for all descendant classes.  So, be careful from where the classes are obtained.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 380 total points
ID: 20344671
Those allocation problems usually found on C++ classes are infrequent in a managed environment, unless you are handling some unmanaged object.
0
 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20345415
pavaneeshkumar:
I really liked the example of the "Executive and Worker", really put it into perspective.

RQuadling:
With the class flattener, when you make a change to the "base" class, do you have to re-gen the files and put back in the extra code again or is it smart enough to just change the required parts?

jaime_olivares:
you were right about the vb ;) and you were right about the examples.  Thats all they showed us @ uni, and then it's fend for yourself.

CraigWagner:
Another email was sent around saying that the "is evil" might of been a bit harsh and said some similar things to you.  I do like your example of the utility class btw.

HonorGod:
All classes will have been generated inhouse, just some of the creators might not still be around.


0
 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20345418
Oh and sorry for taking so long to reply, i've been waiting for a time when i could sit down and really think about what you wrote.  I've skimmed across the posts as they came but wanted to give them the time they deserved.

Thanks for all the help =)
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20348959
The flattening is a process you run to build your classes. Like compiling. You do this prior to running the application. Sure it is another step to take for development, but at runtime, 1 object does 1 task and is flat - no inheritance, dependencies, etc.

There would be little point in having the additional overhead of at run time - though, caching could be used to deal with it. The issue would be that the application would be running, effectively, untested classes.

abstract base
class x extends base
class y extends x implements singleton

A flat y is singleton_y_x_base (just an example).

Now say I could flatten at runtime, the new class would not be code I had written. If the flattening was done at runtime for development only, then that is fine as long as the flattened classes are the published ones.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question