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.

No comments:

Post a Comment