Should I Use Composition or Inheritance?
This is a topic with almost as much tension as database normalization. Many people lean toward composition over inheritance. Here is an example of the difference.
Class Car extends Engine {
var doors;
}
This design assumes you can’t have a car without an engine, inheriting from the engine class. Another way to handle the same problem is through composition.
Class Car {
var Engine;
var Doors;
}
This method, using composition, allows you to change the engine dynamically as well as the number of doors.
Most object oriented gurus lean toward Composition over inheritance; they do not rule out inheritance as a valid option, but restrict the usage to specific scenarios where there is a real relationship between the two objects.
As I am developing a deeper understanding of this principle there are a couple things I find really interesting. I read an article on MSDN a few years back (ok, over a decade) where the VB team was defending their position that the ability to perform inheritance was not a good software practice. They were being bashed because VB couldn’t inherit and didn’t have polymorphism.
Here we are, a few years later saying the same thing…only now with C#, Java, etc.
VB also said they didn’t need the ability to have overloaded method signatures. They met the need by having optional parameters in a method signature.
It turns out that optional parameters are now a part of .Net, with a lot of kicking and screaming on the part of the .Net development team.
I just find it ironic how many times we come full circle.
Even still, you will find it helpful, in your software development, to dig into the difference and value of Composition over Inheritance. This is the key concept in the L out of the SOLID principles.
Health Tip Replies
Sam:
I found your article interesting. I was hoping to see more on vision-care though. I often try to focus on a small object across the room and then on something closer or trace a the lines of a pattern in a picture or drawing after a few hours of staring at my monitors intently. I am not sure how much that practice is helping to maintain my 20-10 vision.
Llewellyn
I do a lot of little things you might find interesting, or possibly a bit extreme 🙂
Dvorak Keyboard – 20 times less finger movement http://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard
Pomodoro’s 25 minutes of work, 5 minute break – http://www.pomodorotechnique.com/ Trackman – http://www.logitech.com/en-us/mice-pointers/trackballs/devices/7365
Kneeling Chair – have to be careful with these, you want the ones where the butt is angled, not flat like a normal chair. I use this when sitting, and it helps my back a lot, but better still is…
Treadmill Desk –
http://llewellynfalco.blogspot.com/2010/11/my-treadmill-desk.html
Stay happy & healthy
Send your tips and comments to btaylor@sswug.org
Cheers,
Ben
$$SWYNK$$
SelecTViews
With Stephen Wynkoop
In this edition:
- New data breach(!)
- Exceed on your deliverables
- Business Intelligence tips
- Facebook Question of the Day
Featured White Paper(s)
Query Tuning Strategies for Microsoft SQL Server
Written by Quest Software
When you’re looking for a reliable tool to diagnose … (read more)