Main features of Object Oriented Languages – Classes, Objects, Encapsulation, Polymorphism, Inheritance

1. Classes and Objects

In an object oriented programming language, we code the program as a corporate collection of objects which are instantiated from various class abstractions. A class is only a logical abstraction and has no physical existence. An object is a specific instance of the class and it has physical existence inside the memory of the computer, when instantiated from the class.

2. Encapsulation

Encapsulation refers to protecting data and code within an object from outside interferences or misuse. The data and code for a particular object are defined in its class abstraction and its data could only be accessed by its member functions, thus isolating the data from the other parts of the program. Such encapsulated data and code within a class abstraction could be included in class libraries and this facilitates code reuse.

3. Polymorphism

Polymorphism is defined as one interface and many methods. An object oriented programming language supports compile polymorphism via function / operator overloading and run time polymorphism via use of virtual functions in inherited classes.

4. Inheritance

Inheritance is the most basic feature of any object oriented language. Inheritance refers to the derivation of sub classes from a base / super class. The base class implements the common features while the sub classes implements their specific features. Using the property of inheritance, we could create a hierarchy of sub classes inherited from the base class.

Simple Class / Object explanation in Java

The following class shows some abstract features of a Fruit –

  class Fruit {                           private: // data encapsulated within class, which is private to the instances of the class

                                           char name[80];

                                           char colour[80];

                                           int weight;

                   public://public methods which could be accessed outside the class

                                      //set_property is protrayed as an example for Polymorphism by function overloading. It uses single interface which is defined as various methods for the given operation

                                        void set_property ( char n[], char c[]) { name = n; colour = c; }

                                         void set_property ( int w ) { weight = w; }

      }

//Creation of simple object banana of class Fruit

     banana = new Fruit();

//Creation of Derived class

  class seed:public Fruit{}

Copyright (c). www.getallarticles.com – 2001