OOP Terminology

July 06, 2008

Let me start off by saying that I'm not an OO expert. I started out with ColdFusion 4.5 when it didn't even have functions, so naturally I learnt procedural programming and struggle with the concepts of OOP (Object Orientated Programming). Ben Nadel has recently started blogging about his OOPhoto project, as he attempts to learn OOP. I thought I'd start blogging about the terminology and concepts behind OOP as I learn it. So here goes!

Inheritance

Inheritance is when one object extends another. This allows you to start with a fairly general object which you can then extend with one of more objects to create something very specific.

For example:

Person object methods

Employee object methods (extends person)

Secretary object (extends employee)

Chef object (also extends employee)

In the above example we start off with the person object which has properties which apply to all people (gender and date of birth). This is extended by the employee object. Because the employee object extends the person object it inherits the properties and methods of the person object (gender and date of birth). We can take this further and create two new objects which are much more specific variants of an employee. Because secretary and chef extend employee, they also extend person.

What does this all mean?

Our Chef object has the following methods:

Our Secretary has the following properties:

Why is the useful?

It means that we can reduce duplication of code as we can create simple objects and then, through inhertiance, make more detailed versions to suit our needs.

Polymorphism

This happens as a direct result of inheritance. What it means is that you can treat the inherited methods and properties in a child object exactly the same as we would if we were interacting with the parent object's method directly. The inherited getGender() method of our secretary object is Polymorphic as it behaves exactly the same as the getGender() method of out chef object.

If you're a bit confused by what I mean by parent and child, then the child inherits (by extending) from the parent object.

Encapsulation

Encapsulation basically means hiding the internal workings. Other objects don't need to know how the employee object calculates the salary. Salary may just be a fixed value or it may use a complex formula, either way, other objects don't need to know how, just what the value is. One of the golden rules of encapsulation is that an object's data should not be exposed directly.

Why is this useful?

Because the internal workings are hidden and not important to other objects, we are free to change it without breaking anything else. How many times have we written some code and then had to change it later. Encapsulation to the rescue!

 


11 comments

  1. @John,

    Good stuff. And thanks for taking notice of OOPhoto. I am gonna start working on the domain model and all feedback would be great!

    Comment by Ben Nadel – July 07, 2008
  2. Hi Ben! I shall be following OOPhoto closely. I actually decided to write this post after reading the comments on your initial OOPhoto post and thinking "what the heck are they saying!". I figured I'm not the only one who finds it hard to learn OOP just because they don't know the terminology. (or maybe I am!?)
    :)

    Comment by John Whish – July 08, 2008
  3. @John,

    You are definitely *not* the only person who has trouble with this. And even more trouble is not so much understanding the terminology (although that is trouble), is bringing it all together in a logical way.

    I get very mucked down when people start talking about Service objects and service layers and gateways and that sort of thing. I still don't feel like I have a great handle on what all those do and I see that many people seem to refer to things very differently.

    Comment by Ben Nadel – July 08, 2008
  4. @Ben, totally agree with you that different people use different terminology to describe layers/objects/gateways/what-they-had-for-lunch. It seems to depend if they have a Java background or have just jumped into CF OOP! cfinterface is a good example - it is key in Java, but not that useful in ColdFusion.

    Comment by John Whish – July 09, 2008
  5. John,

    It's great to see that you are posting about your OOP experiences with Coldfusion because it is something that I am attempting to get to grips with too.

    I have also been reading a set of posts on Adrian Moreno's blog which I found useful.

    www.iknowkungfoo.com/blog/index.cfm/2007/8/22/Object-Oriented-Coldfusion--1--Intro-to-Objectcfc />
    Simon

    Comment by
    Simon Bingham – July 15, 2008
  6. Hi Simon, Thanks for the link :)

    I've seen the posts on Adrian Moreno's blog which are a good into to DAOs and Gateways. From my limited knowledge of OOP, it looks like the posts just talk about the "Persistence Layer" (accessing data which is in a stored format i.e. database/XML file) which is only one part of true OO design.

    Have a look at Ben Nadel's diagram at:
    www.bennadel.com/blog/1286-OOPhoto-Revisualizing-The-Application-Layers.htm
    and then imagine another layer beneath the domain model(s). (Feel free to comment Ben *grin*)

    I'd also recommend reading a great article by Hal Helms about this:
    www.halhelms.com/index.cfm?fuseaction=newsletters.show&issue=122904_badOO />
    I asked Hal Helms about good books and he recommended "Object-oriented Technology: A Manager's Guide" by David Taylor, which I've just bought but it hasn't arrived yet. Watch this space for my brain exploding!

    Comment by
    John Whish – July 15, 2008
  7. @John,

    I'm still working on the Service layer. I've hit some big obstacles there. Not even ready to journey down father into the domain yet :(

    Comment by Ben Nadel – July 15, 2008
  8. @Ben - thanks for the reply :)

    Just wanted to check that I wasn't misinterpreting your diagram. I totally understand your frustration - going OOP just seems like learning to develop all over again!

    Comment by John Whish – July 15, 2008
  9. Word up! It's like trying to learn to walk backwards on my hands :)

    Comment by Ben Nadel – July 15, 2008
  10. @Ben - can't you do that yet?!

    Comment by John Whish – July 15, 2008
  11. Ha ha, working on it ;)

    Comment by Ben Nadel – July 15, 2008

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.