[Tutor] [Slightly OT] Inheritance, Polymorphism and Encapsulation

Stephen Nelson-Smith sanelson at gmail.com
Wed Sep 19 07:51:11 CEST 2007

Michael Langford wrote:

> Inheritance: Syntactic sugar that's not really needed to make a well
> organized system. Often overused, especially by programmers in big
> companies, beginning students of programmers, green engineers, and
> professors. In practice hides a lot of data, often making behavior
> surprising, therefore harder to maintain. Can be used in limited situations
> to great advantage, but like cologne on car salesmen, is used in greater
> amounts than it should be. One should always ask, can I make a simpler
> system with composition.

Pretty much exactly what my colleague said.  Having thought about it I
understand this.

> Polymorphism: The process of creating many classes which a single interface
> which are then all used by an object that doesn't know or need to know the
> type. Many people think you only get this by using inheritance and therefore
> use inheritance many places a simpler, less opaque, more lightweight
> solution will work. Most dynamically typed languages (most notably, python,
> ruby and smalltalk) don't even require you specify the interface explicitly
> to get polymorphic behavior.  C++ templates can do non-explicit interface
> polymorphism, however in a more complicated, blindingly fast to run,
> blindingly slow to compile way.

Also what my colleague said!  This is the bit I had missed.  Perhaps I
need to rethink / reload my understanding of polymorphism.

> Encapsulation: The process of taking what shouldn't matter to the external
> world, and locking it behind an interface. This principle works best when
> put into small, specialized libraries and designed for general use, as this
> is the only encapsulated form that is shown to last over time. Supposedly
> something OO based design allows, but in reality, the coupling among classes
> varies in differing amounts. The module/public visibility of Java is a good
> compromise with classes that hides some data but share some with certain
> other classes. C++ has large issues for historical reasons on this front, as
> the implementation section of a class is largely revealed through the class
> definition.

Interesting.  And again revealing of a weakness in my understanding.

I do think this is a good question for getting a sense of where a
person's understanding is.  I wonder how much this understanding is a
pre-requistite for being a good developer... not too much I hope!

>      --Michael


More information about the Tutor mailing list