Categories

The Basics Of Object Oriented Programming

28

As I pointed out in my previous post, we use classes in Object Oriented Programming(OOP) which are a blueprint of objects that share common properties. This use of classes & more precisely, of objects makes the process of programming easy & efficient.

That was exactly the reason why the Object Oriented Programming Paradigm was introduced. Now, there are certain fundamental features that you would find in every object oriented programming language.

Lets now take a look at each of them individually,
Lets take up CLASSES first. They consists of entities called objects-or they are blueprints of objects.

For example, a car would be a class & a van would be an object that belongs to the class called cars. Now, once we know the behavior of this object, we can use it anywhere we need in the program without having to define the behavior/properties again & again, which is the basic aim of OOP.

The objects here act as building blocks of the program. Though the objects can differ in terms of specific attributes(like a van could be red or blue), the classes would only depict the common behavior of all objects(like each car would have 4 wheels, turn left, right & so on…)

Now, the basic features of OOP…

A) Polymorphism – It simply implies “something having various forms”.
In the OOP world, this is evident with variables & functions.(and hence, with objects as well)

For instance, A variable called MEMBERID could take a name or a number & the program would recognize & accept both.

Another example is the “+” sign which can denote the mathematical operation, or strings or lists.

B) Inheritance – It is simply forming new classes(derived classes) from previously existing ones(base classes). In the process, the derived classes inherit certain properties of the base classes.

The concept is similar to how children inherit certain features from their parents, hence, the base classes are also sometimes refereed to as ancestor classes.

In terms of relationships, we call the relation between the base & derived class the as-is relationship. Consider the base class “car” & the derived class “bmw”. Then we can say “bmw” is-a “car” which is generalized to “derived class” is-a “base class”.

Encapsulation, Data Abstraction & Information hiding are very similar concepts & people often confuse them to be the same.

In one single statement, Abstraction is a technique that lets us know what information should be visible, and what information should be hidden. Encapsulation is the technique to display the information in a way as to hide what should be hidden, and show what’s needed.
Then, Information Hiding is the process of hiding all the inessential details of an object.


More on OOP : Concepts with Examples (By Mamta Paul)

OOP is abbreviated as object oriented programming. Before C++ was introduced, programming languages were procedure oriented languages. But with the advancement of new features, languages are now object-focused. First of all, what this object is. An object represents an entity from real world. Eg: We can create employee, student, book as objects. Classes are group of objects classified on the basis of their common attributes (characteristics). Thirdly, methods are functions that determine behavior of objects. Eg: for a class book, methods can be publish(), edit() etc.

Now, let us try to understand what these new features of OOPs are:

(1) Encapsulation: It means encapsulating or protecting from the outer world. It is basically binding data members and functions together. Eg:

Private Class book

{

                        Int edition();

                        Char Publish():

                        Int x;

                        Char y;

}

Class page

{

                        Int pageno;

}

Here edition() and publish() are the functions which are binded with data members x and y under a single class book. By declaring the book class private, it is encapsulated from the outer class – page.

basics-of-object-oriented-programming-2

(2) Abstraction: It generally means some data is visible and the rest is not. In OOPs, classes can be abstracted. That is, we can make essentials features of a class to be accessible and the rest inaccessible to another class. OOPs give the advantage of protecting the data of a class from unauthorized access by another class. Data of a class can be protected by using the words: private, protected etc.

Eg:  class book

            {

                        Public: Int edition();

                                     Char Publish():

                        Private: Int x;

                                      Char y;

            }

            class page: public book

            {

                        Int pageno;

            }

Data members; x and y of book class can not be viewed by page class since these are protected by the word private.

basics-of-object-oriented-programming-2

(3) Inheritance: If we talk in biological terms, it means features inherited by the child from his parents. In the same way, derived class derives features from its parent class.

Eg: Public class book

            {

                        Public Char author, date;

            }

                 Class page: public book;

            {

                        Int pageno;

            }

Page class which is a child class derives the features like author, date automatically from book class which is its parent class.

(4) Reusability: With the addition of OOPs concepts, it has become possible to reuse the old code and to make a new code by adding new features into it. We can make changes on the existing coding rather than writing the whole code again. Actually, functions in OOP are reusable; once created, they can be used again and again in different applications.

(5) Polymorphism: ‘Poly’ means many and ‘morphism’ means many forms, so the word means existence in many forms. In OOP, we can use one object in different forms.

Eg: int sum(int a, int b);

       Int sum(int a, int b, int c);

       Float sum(int x, int y);

Thus the number and types of parameters change but sum function remains same. So, function like sum() can take different forms. Thus, OOP gives the benefit of polymorphism.

RELATED Understanding Inheritance in OOP

Share.

28 Comments

  1. more on Information Hiding & Encapsulation

    Encapsulation is the process of information hiding and encapsulation in OOP is due to interaction of user(… another object). There is only way to get method (operation to be performed on object) execute is the messaging. user doesn’t know about internal myths of object (or blueprint : Class). this property makes encapsulation possible.

    Several authors and OO guru also argue about attributes encapsulation. because in functional programming languages you need to manage data separately in the form of variables (includes local and global) but in OOP each contains its associated data and operations in itself(in the form of attributes and methods generally speaking class members).

  2. Pingback: 10 Major Differences Between C And C++ at Detrix

  3. Pingback: 10 Major Differences Between C And C++ » Durofy

Leave A Reply