How often is Inheritance used?

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.

5

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!)

4

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())
    drawAdminForm();
else if (user.isEditor())
    drawEditorForm();
else
    drawUnprivilegedForm();

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

1

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.

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.

1

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật