I admit that I am a junior developer, and so far I've only built simple web applications in ASP.NET MVC. But I've never had to use the inheritance aspect of Object Oriented Programming in my own classes! It is true that in using ASP.NET MVC I inadvertently use inheritance (any controller I create will inherit from the base controller class), but I am referring here to the conscious use of inheritance in my design of a particular web system.

Is it because I am a bad programmer? Or could it be that inheritance only comes into play in certain scenarios? Don't get me wrong, I understand what inheritance is and how to code using inheritance if I had to.

I just can't seem to find a scenario to use it at. Basically, I am wondering how often it is used in OO programming in general.

  • 14
    Too often, especially too often wrong. If you are unsure if you need it, you probably don't need it. Well, if you want to see some good examples for use of inheritance, I recommend Martin Fowlers Analysis patterns (martinfowler.com/articles.html#id314249) – Doc Brown Mar 16 '13 at 0:19
  • I can't believe I didn't know about Martin Fowler or his site all this time, so much amazing stuff on there. Thanks for the link. :) – J86 Mar 16 '13 at 1:28
  • 2
    @Ciwan Martin Fowlers work is seminal. If you learn it, understand it and put it to practice, your life will be made a lot easier. – Sam Mar 16 '13 at 2:43
  • 3
    Having the same word in the title does not constitute a duplicate. Asking why inheritance never seems to come up is a very different question than asking when you should use composition instead, although they are related. – Karl Bielefeldt Mar 16 '13 at 15:09
  • What you're making use of are the customization points of pre-built OOP framework objects. It is an everyday aspect of OOP usage, but OOP is capable of much more than that. – rwong Mar 20 '13 at 11:35

Inheritance is used all the time when writing object-oriented code. In most OO languages, (with C++ being a prominent exception,) all objects have a single "base object" class that they derive from that provides common functionality, so literally everything that uses an object uses inheritance.

Beyond that obvious point, there are many times when you'll find that you need several different types of object that share a lot of important traits in common. Inheritance is very useful here. For example, if you've ever built a GUI in an object-oriented language, you probably used a framework where all of the graphical controls had a common parent class that handled a bunch of functionality that all the controls in the windowing system they run on need to cover. And if you do any work with parsing or code generation, (which are basically two sides of the same coin) you'll quickly run into other examples where a base class with a bunch of derived classes are useful.

It's true that inheritance is not the only way to make objects work together; you can also use composition, (where one object contains another object of a different type,) which works better in some cases. Be careful about treating this as a point of dogma, though. Inheritance is a very useful tool if you know what you're doing. So is composition, and neither one of them is really a good substitute for the other. The important thing is to know what you're doing, and if you don't, learn and experiment and develop your knowledge and your skills. (And feel free to ask questions on here or StackOverflow!)

| improve this answer | |
  • 2
    There is no universal base class in C++. You can use classes without using inheritance in C++ just fine. – fredoverflow Mar 16 '13 at 10:03
  • 2
    @Fred: I know. I said that. – Mason Wheeler Mar 16 '13 at 13:04
  • 1
    +1 this is a well-phrased answer that is perfect for younger developers just starting out. – Brandon Mar 19 '13 at 0:16
  • Inheritance is only useful for saving keystrokes when you have a composition relationship for which the inner element will never change, and you intend to all of the methods from the inner object on the outer object and delegate to the inner object's methods in each case. It's just often-abused syntactic sugar for a bizarre special case with few practical uses. Use composition + interface-polymorphism for a much better time. – weberc2 Dec 12 '15 at 3:53

Knowing when to use inheritance and when to use other features of the language you are working in takes time. There are a many different ways to tackle the same problem and inheritance is only one tool of many at your disposal.

The popularization of object oriented languages might lead one to believe it is the best way to write and reuse code, but even the first paragraph of the Wikipedia article on OOP inheritance stresses that composition is the preferred and more flexible way to reuse code.

So to answer your question, you are not a bad programming for not using it all the time. Like Doc Brown said, it is used far too often as the solution to a problem when composition could be used instead.

| improve this answer | |

You've already recognized that you frequently use inheritance. What you're really asking is why you never seem to need to create your own new base class.

The reason is that base classes tend to be created most often in frameworks, libraries, and in the early stages of a project when the architecture is just being defined. These sorts of tasks tend to be given to more senior programmers, but even they might not create many new base classes in their code in a framework-heavy job like web development.

And no, it does not mean you are a bad programmer. Quite the opposite, in fact. A lot of new programmers have a hard time distinguishing the proper situation to use inheritance, and have a tendency to overuse it, creating tightly coupled code.

If you're wondering if you're accidentally missing out on opportunities to use inheritance properly, the trick is to look at the calling code for a lot of if or switch statements that do different things depending on the type of a variable. For example:

if (user.isAdmin())
else if (user.isEditor())

Just keep an eye out for code like that, and you'll do just fine.

| improve this answer | |
  • This is the real answer. We ASP.NET programmers tend to spend a lot of time putting controls on pages and fiddling with CSS and so on. Creating classes for business logic is a proportionally smaller part of our work, and we have mature libraries to handle a lot of the plumbing. I've only created classes that use inheritance a handful of times, really. – MGOwen Mar 18 '13 at 23:48

But I've never had to use the inheritance aspect of Object Oriented Programming !

Oh but you have! If you're using ASP.NET MVC you're deriving your controllers from System.Web.Mvc.Controller. This is what gives your controllers all the power and functionality they have without you having to do any heavy lifting.

I just can't seem to find a scenario to use it at. Basically, I am wondering how often it is used in OO programming in general.

For me, I use inheritance quite often but you need to know when you need to use it. There is little point having a god object that you derive everything from; however if you're implementing business logic you might have a base SectionRule class that you define various rules in.

A real world example could be a driving game. You'd have a base abstract class called Vehicle which couldn't be instantiated on its own because it would be useless but you could define basic behaviour such as IsRunning and CurrentSpeed while providing a Start() method that is abstract meaning you need to implement it. You could derive classes Car, Bike and Truck and each would keep track of whether it is running and the current speed, but you manually implement the Start() method for each object type.

The other thing to bear in mind is that OO is used everywhere within most frameworks (ASP.NET, WinForms, WPF, etc), you may be using it without knowing. Most WinForms components derive from Control.

The biggest advantage of using OO and inheritance is reducing the amount of code you have to write for related objects.

| improve this answer | |
  • Question updated after reading your and Mason's reply. Thanks :) – J86 Mar 16 '13 at 1:08

Inheritance is mostly used in framework-based development, where you most often don't define your own classes, but extend existing ones:

  • Asp.Net MVC - Controllers
  • Ruby on Rails - Controllers and ActiveRecords
  • Android - Activities, AsyncTasks, Adapters
  • WPF and Windows Phone - Windows, UserControls, Controls, Pages

If you write a code which isn't related with frameworks, you can do it without inheritance by using the composition pattern.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.