My struggles in understanding and learning about Object Oriented design, and the tools and knowledge I've taken from them.

Thursday, October 9, 2008

Object Oriented Programming (Part 2)

In part 1 of the Object Oriented Programming post, I talked about CLASSES, OBJECTS, METHODS, and PROPERTIES. There's another side of object oriented programming (hereafter referred to as OOP), and it speaks directly to concepts in OOP, versus implementations of it. Specifically, the technical concepts that make up OOP are ABSTRACTION, ENCAPSULATION, and POLYMORPHISM. One could probably include a couple more concepts to make up OOP, but I think the above three, along with the implementation concepts of CLASSES, OBJECTS (and the instantiation of them), METHODS, and PROPERTIES, adequately describe OOP.

This post will discuss ABSTRACTION, ENCAPSULATION, and POLYMORPHISM at an overview level.

ABSTRACTION
ABSTRACTION
speaks directly to the concept of "generic." Anything that is an abstract concept of something more specific is an abstraction. For instance, a shape is an abstract idea (notice a shape is also a noun, and therefore a candidate of a CLASS), and a shape can be implemented more specifically as a circle, square, triangle, etc. Similarly, a Mammal is generally an abstract concept, and a Human, Dog, Cat, or Whale is more specific.

In my experience, ABSTRACTION is implemented in 1 of 2 ways: either an ABSTRACT CLASS or INTERFACE. I won't really go into code examples right now, but think of both ABSTRACT CLASSES and INTERFACES as shopping lists (some people refer to them as contracts between classes). When a class is abstract, it looks an awful lot like an interface, where there is no real instantiation of the thing itself, but rather, other classes use them to have a framework from which to work. More on ABSTRACTION in the next post.

ENCAPSULATION
ENCAPSULATION
is the process of assembling smaller parts into a bigger part...well, that's part of it. The other part is making various components of the class accessible to the "outside world." Words like PUBLIC and PRIVATE are often used to describe this concept. A METHOD or PROPERTY is PUBLIC if it can be accessed from the outside world. A METHOD is PRIVATE if it can only be accessed from within the class.

This is a foreign concept in procedural programming, because everything is sort of working from within a big silo. There is no concept of protecting data from another location. This is really the key concept in OOP.

An example of ENCAPSULATION would be: a driver (Human) can drive a car. The driver needs to know about the steering wheel, the gas pedal, the brake pedal, the shifter, and how to interoperate with all these concepts. The driver does not need to know about how the transmission responds to speed, or the explosions that need to happen in the engine for the car to move, or how the engine gets air and gas injected into it to perform the explosions to make the car move. So the pedals, steering wheel, and shifter are PUBLIC within the car class, and exposed to the driver, and the transmission, injection system, and components of the engine are PRIVATE, and only accessible internal to the car.

POLYMORPHISM
POLYMORPHISM
is a concept that requires some prerequisite understanding of OOP, particularly INHERITANCE, so I will speak to that a bit.
INHERITANCE is the process of one class inheriting methods and properties from a parent class. Logically, you could think that anything more specific than something else is a child of the more abstract concept.

For example, a Human is more specific than a Mammal, so it would follow that a Human would inherit from a Mammal (fur bearing, lungs, eyes, etc). It also follows that a Firefighter or Police Officer could inherit from Human. These are simple examples, but that's the idea.

POLYMORPHISM speaks to the concept of similar behaviors by different classes doing different things. For instance, a Human and a Dog could both inherit from Mammal the capacity to speak, but when Human implements Speak(), it is much different than the way Dog implements Speak() [where Human and Dog are the classes, and Speak is the method].

I'll post on each of these concepts in time, but I wanted the first few posts in this blog to be more conceptual than incorporating real world examples...there's plenty of time for that later.



Digg

No comments:

Followers

Search This Blog

Powered by Blogger.