Object Oriented Concepts: Part 3

General Development Concepts

Classes and how they fit in with Objects


Date : 2006-04-25
Object Oriented Concepts Part 3: Classes

Ok, time to confuse the issue! Well, not really. One of the first things you'll need to know about Classes is that they are very similar to objects. The differences between an object and a class is sometimes hard to see because a class is itself reusable just as an object is reusable but with one major difference - a class is just a schematic of the objects that it houses.

To break it down into human readable terms it works like this: Say you have a collection of objects that do similar things:

Fog Lamp
Table Lamp
Flashlight

All these items provide light in one form or another but we want to address them collectively so we create a class called 'lamps'. The Lamps class then hides the underlying objects so that you dont have to deal with them individually but it still exposes the functionality intrinsic to each one of them. And when including them in your program you dont have to include each object individually, you can include just the class and then access the properties of each object by way of the class. It's really a neat way of keeping things nice and tidy when you have loads of similar objects that function only slightly differently but that you need to access as a whole.

Using Classes:

"But what use are they"? You might ask. Well, heres the thing. Say you want to create an application where you want to be able to carry out several similar functions that are defined in various objects but you dont want to expose the objects themselves. Through assigining a CLASS to a new object of your creation (i.e. MyLamps) you would then be able to use all of the functions defined within the objects that are housed in the class.

Lets try a visual example:

Objects:

* Fog Lamp
-- Light On
-- Light Off
-- Intesity
* Table Lamp
-- Light On
-- Light Off
-- Intesity
* Flashlight
-- Light On
-- Light Off
-- Intesity

Ok, so there are three objects with obviously similar functions. Now we could just include references in our code to them individually but that is messy and decidely NOT OO. So, we need to wrap them up in a CLASS at this point which will then look like this:

Class:

* MyLamps
* Fog Lamp
-- Light On
-- Light Off
-- Intesity
* Table Lamp
-- Light On
-- Light Off
-- Intesity
* Flashlight
-- Light On
-- Light Off
-- Intesity


So there you have it. Now you can define any object you like as a type of class relating to 'MyLamps' and you would be able to get at the underlying objects that are embedded in the class.

There is a lot more that can be said about Classes but I'll leave that to the reader to figure out as he/she goes along. Next time we'll talk about Inheritance.

Comments :

No comments yet
  • Search For Articles