Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Don't Inherit Code

DZone's Guide to

Don't Inherit Code

· Agile Zone
Free Resource

Reduce testing time & get feedback faster through automation. Read the Benefits of Parallel Testing, brought to you in partnership with Sauce Labs.

I learned a long time ago that there are essentially three different kinds of relationship between two classes. They are, in order of increasing tightness:

  1. Uses, in which I care only about your public API;
  2. Creates, in which I also need to know your class;
  3. Inherits/extends, in which I can also see some of your workings and I become part of you.

Don’t do that last one. Ever.

Many of the GoF patterns help move code up this list, from tight towards loose coupling. Except TemplateMethod, which not only encourages inheritance, it also creates a circular dependency by having the superclass only “work” in the presence of extensions. Bad.

Don’t inherit code. Bad.

The Agile Zone is brought to you in partnership with Sauce Labs. Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure.

Topics:

Published at DZone with permission of Kevin Rutherford, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}