I had a brief discussion yesterday which really drove home how far removed I have been from what I would consider core knowledge for a developer. I'm not saying that I had my 'Developer Card' pulled completely, but if it was based solely on the merits of my ability to verbalize those core concepts, it would have been a close call.
I'm not talking about defining Encapsulation/Abstraction, Inheritance or Polymorphism. I believe these concepts are abstract enough that I can provide a definition, and example, to demonstrate. No, I'm talking about implementation details, specifically with C#, and some of the nuances between keywords such as 'abstract', 'virtual', 'override' and 'new'. These are the concepts that I realized were no longer in my immediate 'cache' and had long ago been swapped to disk, and that disk was sorely fragmented!
As part of a refresher course, I'll walk through some of these core skills and techniques and create some examples. I'll post them up here as I work my way through them mainly because once I've written something down, my retention period is much longer.
When I think of encapsulation, I tend to think of hiding implementation details behind some sort of contract. C# examples of this type of contract could be in the form of a method signature (delegates), an interface, or an abstract class. An implementation of this contract should hide the details of the 'dirty work'. This type of encapsulation is usually implemented using some sort of inheritance, which allows us treat collections of different types the same (polymorphism).
When thinking of inheritance, I usually think of two different types: interface and implementation. Interface inheritance is when I have defined a contract (via an interface) - I'm not making any guarantees about implementation - that's left up to the interface implementation. There is also implementation inheritance using a base class or an abstract class. When I derive a class from an existing base class, my new class will inherit any public properties/methods of the base class. My derived class may have the opportunity to extend the base class in some way if any base methods are marked as virtual by use of override. When leveraging interface inheritance, I'm defining a way to plug in different implementations at runtime.
Inheritance usually has overlap with polymorphism. If I have declared an interface, or base class, which defines some method/property, I often want to treat a collection of these instances as one. Polymorphism is the language feature that allows this.
Anyway, I sat down last night and wrote some code to help brush up on implementation of some of these concepts. The code is what I remember from what I call the 'classic shapes' example. An early mentor using the 'shapes' example to demonstrate many of these concepts back in the C++ days, so its still one of my favorites. Others have been the automobile (transport), animal, etc...but shapes is still my favorite because way back then it carried over into a hobby of mine: video programming.
Download the source code from here: BacktoBasics-ObjectOrientedDesignPrinciples.zip.
Any questions, comments, or concerns, please let me know.