Classes and Objects

Encapsulation refers to the wrapping up of data and its associated meaningful functions into a single entity. One mechanism that allows programmers to encapsulate data is called a class.  Classes are also called abstract data types as they enforce the object-oriented programming concepts of data abstraction and encapsulation.

Once a class is defined, one can create variables from it. A class variable is called an object or an instance. In other words, an object is an identifiable entity having certain characteristics and behaviour as defined by a class.

Taking an example, using C++,

class Name {


      //data members and methods that remain private i.e. cannot be accessed outside the class

   protected:      //data members and methods that are protected i.e. accessible from members of their same class and also from members of their derived class (see Inheritance)


    int mem1;    void met1();

    //data members and methods that are public i.e. accessible from anywhere

} Ob1, Ob2;

Here, ‘Name’ is the name of the class and ‘Ob1’ and ‘Ob2’ are its objects. The body of the declaration can contain members, either in the form of data or of function declarations.  These data members and member functions/methods are classified into three categories: private, public and protected. These are reserved words and called access specifiers. As described in the comments, the access specifiers essentially determine where a member of the function can be used in the entire program. By default, all members of a class declared with the class keyword have private access unless otherwise specified.

To access the class methods and data members, objects are used as follows:

Ob1.met1(); //accessing public member function met1() through the first object ‘Ob1’

cin≫ Ob2.mem1; //accessing public data member mem1 through the second object ‘Ob2’

Now how does it work in the memory? For each object of the class Name, a different set of values can be stored. This means mem1 for Ob1 (accessed by Ob1.mem1) can store, say, 4, while mem1 for Ob2 (accessed by Ob2.mem1) can store, say, 20. To relate, one can compare this with a school classroom list – here, the C++ class becomes ‘A’ (for classroom section) and it can hold the names of students, with their roll numbers, personal information, marks in different subjects, etc. Thus, classes can be used to streamline storage of students’ details while selectively privatizing personal information.

See also: Inheritance, Polymorphism, Why Object-Oriented Programming is better?


Inheritance is the capability of one class of things to derive its properties from another class of things. It is an essential object-oriented programming concept.

Let’s think of inheritance from a real-life perspective – take, for example, your family. You derive some of your characteristics from your father and some from your mother. Some of these characteristics are salient, while some are prominent like the color of your hair or eyes. Even further, your father and mother derive their characteristics from their parents.

Now, how exactly does this translate into code?

Well, we have all sorts of real-world relations that inheritance can describe in programming such that some features are salient while others are visible to the user clearly. This is implemented through inheritance, having different access levels (private, public and protected).

Let’s consider another example to understand this better –  a class called ‘Shape’ defines the basic form of shapes like circles, triangles, and squares. Thus, classes ‘Circle’, ‘Triangle’  and ‘Square’ inherit from ‘Shape’ and add onto the basic structure on the basis of their individual features i.e. the specific areas, perimeters, etc of the particular shapes. Similarly, ‘Equilateral Triangle’, ‘Isosceles Triangle’ etc become derived classes from ‘Triangle’ with the specific where the definition for the sides changes accordingly.

Types of inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
Types of Inheritance
Types of Inheritance

Inheritance has several advantages as follows: reusability, real-world relevance; time-saving; transitive nature; easier debugging.