Monday, June 23, 2008

Classes

A class is the fundamental element of OOP.

So what is a class?

Well, Object Oriented Programming (OOP) is based on creating and manipulating objects. To create these objects classes are used. A class is basically a definition for an object. It will define the characteristics of the object that will be created using that class. In other words a class is a mould that is used to create an object.

A class consists of attributes and methods. Attributes will hold data and methods will process those data to give the required output.

Basically OOP has three fundamentals when defining classes.
  • Encapsulation
  • Inheritance
  • Polymorphism


Encapsulation

This means hiding or restricting access to certain data within the class by the outside world. This is done by using access modifiers. These access modifiers will define what part of the program has access to that specific data and what is not. Access modifiers will differ slightly according to the programming language. “Public” and “Private” are two access modifiers that you can find in almost all OOP languages.

When you define a method or an attribute as public it means that methods outside the class can also access those methods or attributes. When you give the private modifier to an attribute or a method, it means that only methods inside the class can have access.


Inheritance

Inheritance normally means one class inheriting the attributes and methods of another class. So when you create an object from the class that object will have attributes of the inherited class as well as the attributes of the class that is used to create that object.


Polymorphism

Polymorphism means many forms. In classes this is exactly what is done using this concept. Polymorphism is used to manipulate methods. And there are two basic ways of dealing with polymorphism.

Overloading
This means that you can define many methods with the same name but with different method
signatures. Remember, OOP doesn’t allow two methods with the same signature to be in the
same class.

Overriding
When you inherit a class and create another, you can define methods with the same signature
as the methods in the inherited class, in the class you define. This will override the
functionality of the method in the inherited class.

Tuesday, June 17, 2008

Object Oriented Programming

Object Oriented Programming or shortly known as OOP is a programming concept, in which the structure of the program is designed as objects representing real life objects in the business case.

OK, here is what it really means.

When you create programs using OOP what you do is that you categorize the logic of the whole program into objects.

Think that you are building a program for registering students for courses. Then you can define objects such as Student, Registration Officer and Course. The important thing to remember when you define objects is that they should actively participate in the program.

In OOP programming, logic is handled by objects. When you take an object it consists of following categories.

  • Properties - Define how the object will look.
  • Methods - Define how the object will behave.

Take the above student registration system. If you take the Student object it has properties such as

  • Name
  • Age
  • Gender
  • Date of Birth

And it has methods such as

  • Select Course
  • Pay Money
  • Get Balance


OOP is easier to implement because it normally represent the real world inside the computer. After defining objects it is a matter of manipulating them in a logical manner to achieve the desired output.

How do you define objects?

Well how do you create a ceramic vase if you given the job?

You would first create a mould to create the vase. Won’t you?

The same applies to creating objects.

The mould for an object is called a Class.