Anda di halaman 1dari 4

2 - Principles of Object Oriented Programming

A program written in object oriented style will consist of interacting objects. For example for a school database we may have many

students, teachers and classroom objects.

In order to be able to create an object, we must provide a definition called a class. A class is a kind of mould or template that dictates what objects can and cannot do. Once a class is defined we can create as many objects of the class as a program requires. Thus an object is said to be an instance of a class.

In writing object oriented programs we must first define the classes and while the program is running we use these classes so that objects are created to accomplish the tasks required.

To instruct a class or an object to perform a task we must send a message to it. For a class or an object to process a message it

receives, it must possess what is called a matching method. A method is a sequence of instructions that a class or an object follows to perform a task

All object oriented programming languages have the following 3 traits in common:

1. Encapsulation :
a. A programming mechanism that binds together code and the data it manipulates in such a way that a self contained unit is created b. It also keeps both code and data safe from outside interference and misuse c. When code and data are linked together in this way a class is constructed with which the required objects are created d. The data defined by the class are referred to as instance variables and the code that operates on the data is referred to as the methods. e. Eg. class employee { private int idno; private string surname; private string Name;

public void setIdno(int idno) { this.idno=idno; } Public int getIdno() { Return idno; } }

2. Polymorphism :
2

a. Reduces complexity of programs by allowing the same interface to be used to specify a general class of action b. The compiler then selects the specific action as it applies to each situation c. Sometimes the nature of an object cannot be known beforehand and can only be decided during runtime. d. Polymorphism allows for methods to be created which can be applied on different types of objects irrespective of their nature. e. Ex. Consider the sqr() function which returns the square of a number. In languages which do not support polymorphism, we would need say three such functions lsqr() which returns the square of a long integer, fsqr() which returns the square of a float-point value and isqr() which returns the square of an integer. Although the underlying concept is the same one would still need to remember three names. Due to polymorphism this need not be since all three functions would have the same name sqr() and then during run-time depending upon the type of argument passed, the appropriate version of sqr() would be called

3. Inheritance :
a. Inheritance is the process by which one object can acquire the properties of another object b. This feature supports the concept of hierarchical classification c. It makes it possible for an object to be a specific instance of a more general case by defining those qualities that make it 3

unique within its class and inherit its general attributes from its parent

Anda mungkin juga menyukai