Heuristics on when should one subclass and when not [closed]

Are there any good tips, when one should subclass and when should not?

It’s clear that we should not subclass when

  • we want to reuse a single method of some class
  • we override all methods, changing the behavior of superclass completely.

But in real life, it’s more difficult to decide. For example, I have a BasicToolbar class, which has several buttons arranged on a panel. There is a second class ExtendedToolbar, which hould

  • have some additional buttons
  • change the ordering of all buttons
  • on condition hide/remove a couple of buttons from parent class

I tend to subclass to reuse those buttons and override ordering/addition logic, but occasional removing of a button of the parent class seem to be against idea of inheritance.

Any opinion is welcome.

8

The Liskov Substiution Principle is by far the best “heuristic” I know of for determining whether direct subclassing is a good idea.

Say you have a base class Foo, and derived classes Bar1 through Bar9. In a nutshell, the LSP states that any block of code using a Foo must work correctly no matter which of the nine Bar types that Foo actually is.

From the user’s point of view, that means code using the base class should be able to work almost exclusively with that base class, without caring what the actual type of the object is.

From the implementer’s point of view, that means each subclass must implement the full interface of the base class, including all of the implied semantic properties, without any surprises or additional restrictions.

If the subclass you’re thinking of writing won’t follow these rules, that’s a very strong indication you should not be using inheritance.


Regarding your toolbar example, I don’t think it makes a lot of sense to use inheritance that way. I’d rather have Button classes, a generic Toolbar class that can contain any arrangement of Buttons, and then make BasicToolbar and ExtendedToolbar be objects with a Toolbar member that they add Buttons to in their constructors. If they share a bunch of buttons, make BackButton/LikeButton/UpvoteButton classes with a Button member, and let the Toolbars contain them. Using inheritance anywhere in this arrangement will only add unnecessarily tight restrictions on how you can rearrange these classes in the future.

2

It depends on the conventions and guidelines for a specific programming language. In C# it is not easy to delegate methods to an inner object (it requires explicit code). So inheritance can be easier. When creating a public .net based API, inheritance is often recommended, to simplify discovery for the API user.

One common use is when you want to use the template method pattern.

I that pattern you write the skeleton of an algorithm in an abstrac class. In some parts of that skeleton you can certain methods that are not implemented.

Subclassed implement those methods completing the algorithm. That can also be achieved without inheritance using injection and or delegates.

In general you should not subclass where there is not “is a” relationship between class.

Another patter that uses inheritance is the decorator patter which is similar to the toolbar example you gave.

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