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


Encapsulation : a. 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. public void setIdno(int idno) { this.All object oriented programming languages have the following 3 traits in common: 1. private string Name.idno=idno. A programming mechanism that binds together code and the data it manipulates in such a way that a self contained unit is created b. } } 2. } Public int getIdno() { Return idno. private string surname. It also keeps both code and data safe from outside interference and misuse c. class employee { private int idno. When code and data are linked together in this way a class is constructed with which the required objects are created d. Eg. e. Polymorphism : 2 .

the appropriate version of sqr() would be called 3. 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 . Sometimes the nature of an object cannot be known beforehand and can only be decided during runtime. e. Polymorphism allows for methods to be created which can be applied on different types of objects irrespective of their nature. Ex. d. Inheritance : a. Although the underlying concept is the same one would still need to remember three names. fsqr() which returns the square of a float-point value and isqr() which returns the square of an integer. Reduces complexity of programs by allowing the same interface to be used to specify a general class of action b. 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.a. This feature supports the concept of hierarchical classification c. The compiler then selects the specific action as it applies to each situation c. In languages which do not support polymorphism. Inheritance is the process by which one object can acquire the properties of another object b. Consider the sqr() function which returns the square of a number. we would need say three such functions lsqr() which returns the square of a long integer.

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

Sign up to vote on this title
UsefulNot useful