Object-Oriented Programming

Back

Object

Object is a representation of something that has been defined first. Look around you, everything is an object. You yourself are an object.

Every object is type of something.

A Mercedes that you drive everyday to work is an object of type Car, and it has been given a name Mercedes, and it has color red. There are many other objects of type Car that has many different names, and different colors. You are an object of type Human, and you have a name. Of course, to identify every object of type Human, they all have different names (but can be the same), and more, every object of type Human have different properties. Some of us (we are all type Human), has brown eyes, some has blue eyes. Some has curly hair, other may has blonde hair. These properties, may be different or can be the same for each objects of the same type. But anyhow, there must be some properties that is unique to each object. Well.. for human, we all have different thumb print, and each one of us have been given a unique social security number by our government so that they know you are not an illegal immigrant.

So, you know that objects have properties.

Properties of an object, can be use as an identification of the object, and can also determine the object's condition. I have a red car, while my neighbour has a black car. My car can run faster then my neighbour's car.

Property is a name with value, as shown below:

	my car
	color = red
	speed = 160 km/h
	
	my neighbour's car
	color = black
	speed = 50 km/h

Beyond the properties, there are something else about the objects...

You may step on the car's pedal so that it will accelerate and you will arrived at the airport faster, and you step on the car's brake when there's a chicken crossing the road, so that your car will stop before it run over the chicken. These actions - accelerate and stop is the car's behavior.

Therefore, object's has behaviour. Behaviour is what an object can do. Objects of type Human can walk. Objects of type Bird can fly. Objects of type Fish can swim. Objects of type aeroplane can also fly.

For every of those actions - walk, swim and fly, there actually an implementation of how each objects did the actions. How does the car accelerate? This behaviour might result by pumping more petrol to the engine, and... (Well, I don't know more detail about this, i'm not a car mechanic).

Behaviour has a name to describe it, and it has an implementation details.

	a car's behaviour
	
	accelerate
		... pumps more petrol to the engines... increase speed..
	
	an aeroplane's behaviour
	
	fly
		... a pilot controlling the wings
	
	a bird's behaviour
	
	fly
		... flapping both wings	
	

For now, you know that object has properties and behaviours. Property is a name with value, while behaviour also has a name to describe it, and has an implementation details.

Class

You already know that an object is a representation of something that has been defined earlier - a template of an object. Before any object can exist, there should be a template or a blue print of how an object should be. It is more like when you want to built a house. The house plan, or blue print of the house, must be completed first. This blue print, of course, has all the description details of how the house should be - how many rooms, the size of living room, and other rooms, how many doors and other things. A house developer will start building a house according to the description of the blue print. (You may agree with me that, a house does not has behaviour, but only properties).

Objects are built from templates. Templates or blue prints describes about the objects - properties and behaviours.

Now let see things this way. You can group objects together, so that you have a group of objects of the same type. You pick an object, examine its properties and behaviours and put it into it's appropriate group. You pick another object and do the same. And you continue doing this with all the objects in this world... When you do this sort of things, actually you are doing classification - you classify those objects. Thus, a Class is a description of a groups of objects properties and behaviours.

Object comes from a Class. Object is an instantiation of a Class. A Class is something that does not exist, it is only an idea or abstraction of an object, while an Object is something that does exist, because it's properties has values, and it's behaviours can do something.

Lets define a class Cat.

	a class Cat 
		properties
		name has any name
		fur_color has any color
		
		behaviour
		talk 
		  - say "Meowwww..."
		run
		  - with four legs
		eat
		  - catch a mouse first then kill and bite with teeth
		
The class Cat describe that, a Cat object will has a fur_color of some color and has a name. It also implement how the Cat object should talk, run and eat.

What would the cat object be like. Let say, we have two objects of type Cat: my_cat and my_neighbour_cat

	my_cat.name is "Salina"
	my_cat.fur_color is black
	When my_cat.talk, it will say "Meowwww..."
	
	my_neighbour_cat.name is "Comot"
	my_neighbour_cat.fur_color is white
	When my_neighbour_cat.talk, it will say "Meowwww..."	

To differentiates between objects of the same class, they have different properties, but they have the same behaviours. Well, you can say that they have the same behaviours, but behaviour may be controlled by certain properties. Let add one more property to the class Cat, the property age, so that, the behaviour run is depend on the value of property age.

	my_cat.age is 1 year
	my_neighbour_cat.age is 10 years
	
	because of the property age, 
	my_cat.run faster then my_neighbour_cat.run
	
	

Abstract Class

There exist a class where you can't create an object from it. If an object would have been created from such a class, that object actually would not exist because it is abstract. For example, you can create an object from class cat, dog, or fish, but you should not create an object from the class animal. In the real world, you never see an object that is an instance of animal, but what you see is, an objects that are instances of cat, dog or fish. Class animal is generic, it describes other class of type animal, like cat, dog or fish.

OK... class can describes other class. Usually, a class that describes other class are an abstract class. Like animal, vehicle is an abstract class because its describes car, truck and van.

Abstract class will describe a generic properties and behaviour of classes that it's describes. Below, we define an abstract class animal, that have one property - name, and one behaviour - talk.

	abstract class animal
		properties
		name
		
		behaviours
		talk

Lets use the word extends to indicate that a class is described by another class. Class bird extends from class animal, so that class bird will also has the property name, and the behaviour talk, that was declared in the abstract class animal. Class bird also has one more behaviour, fly that is not declared in the class animal (because not all animal fly).
		
	class bird extends animal
		properties
		name
		
		behaviours
		talk
		fly

You must have been wondered that, class bird should be an abstract class too (?). Well, I'll let you think of that.

< CONTINUE... >



Back
Hosted by www.Geocities.ws

1