top of page

Object Oriented Programming Language? Features and Characteristics

Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.



Building blocks of OOP

Next, we’ll take a deeper look at each of the fundamental building blocks of an OOP program used above:

  • Classes

  • Objects

  • Methods

  • Attributes

Classes

classes are essentially user defined data types. Classes are where we create a blueprint for the structure of methods and attributes. Individual objects are instantiated, or created from this blueprint.


Classes contain fields for attributes, and methods for behaviors. In our Dog class example, attributes include name & birthday, while methods include bark() and updateAttendance().

#include <iostream>
#include <string>
using namespace std;

class Class {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};


Objects

Objects are instances of classes created with specific data, for example in the code snippet below Rufus is an instance of the Dog class.


Code:

#include <iostream>
#include <string>
using namespace std;

class Class {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};

int main() {
  Class Object;  // Create an object of MyClass

  // Access attributes and set values
  Object.myNum = 15;
  Object.myString = "Example of Defining the Object in OOPs";

  // Print values
  cout << Object.myNum << "\n"; 
  cout << Object.myString; 
  return 0;
}

Output:








Attributes

Attributes are the information that is stored. Attributes are defined in the Class template. When objects are instantiated individual objects contain data stored in the Attributes field.


The state of an object is defined by the data in the object’s attributes fields. For example, a puppy and a dog might be treated differently at pet camp. The birthday could define the state of an object, and allow the software to handle dogs of different ages differently.


Methods

Methods represent behaviors. Methods perform actions; methods might return information about an object, or update an object’s data. The method’s code is defined in the class definition.

When individual objects are instantiated, these objects can call the methods defined in the class.


Code:

#include <iostream>
using namespace std;

class Class {         // The class
  public:               // Access specifier
    void myMethod() {   // Method/function
      cout << "Welcome!";
    }
};

int main() {
  Class Object;     // Create an object of MyClass
  Object.myMethod();  // Call the method
  return 0;
}

Output:






Four Principles of OOP

The four pillars of object oriented programming are:

  • Inheritance: child classes inherit data and behaviors from parent class

  • Encapsulation: containing information in an object, exposing only selected information

  • Abstraction: only exposing high level public methods for accessing an object

  • Polymorphism: many methods can do the same task


Inheritance

Inheritance is the procedure in which one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class. And the class that inherits the properties from the parent class is the Child class.


There are broadly 5 forms of inheritance based on the involvement of parent and child classes.

  1. Single Inheritance: This is a form of inheritance in which a class inherits only one parent class. This is the simple form of inheritance and hence also referred to as simple inheritance.

  2. Multiple Inheritance: An inheritance becomes multiple inheritances when a class inherits more than one parent class. The child class after inheriting properties from various parent classes has access to all of their objects.


Code:

#include <iostream>
#include <string>
using namespace std;

// Base class
class Vehicle {
  public: 
    string brand = "Ford";
    void honk() {
      cout << "This the Example of Inheritance in OOPs \n" ;
    }
};

// Derived class
class Car: public Vehicle {
  public: 
    string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}

Output:







Encapsulation

Encapsulation is defined as binding together the data and the functions that manipulates them. Encapsulation means containing all important information inside an object, and only exposing selected information to the outside world. Attributes and behaviors are defined by code inside the class template.

Then, when an object is instantiated from the class, the data and methods are encapsulated in that object. Encapsulation hides the internal software code implementation inside a class, and hides internal data of inside objects.

Encapsulation requires defining some fields as private and some as public.

  • Private/ Internal interface: methods and properties, accessible from other methods of the same class.

  • Public / External Interface: methods and properties, accessible also from outside the class.


Code:

#include <iostream>
using namespace std;

class Employee {
  private:
    int salary;

  public:
    void setSalary(int s) {
      salary = s;
    }
    int getSalary() {
      return salary;
    }
};

int main() {
  Employee myObj;
  myObj.setSalary(50000);
  cout << myObj.getSalary();
  return 0;
}

Output:







Abstraction

Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts. It is one of the most important concepts of OOPs.


Code:

using System;

namespace MyApplication
{
  // Abstract class
  abstract class Animal
  {
    // Abstract method (does not have a body)
    public abstract void animalSound();
    // Regular method
    public void sleep()
    {
      Console.WriteLine("Zzzzzzz");
    }
  }
  
  // Derived class (inherit from Animal)
  class Pig : Animal
  {
    public override void animalSound()
    {
      // The body of animalSound() is provided here
      Console.WriteLine("Sleeping Time");
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      Pig myPig = new Pig();  // Create a Pig object
      myPig.animalSound();
      myPig.sleep();
    }
  }
}

Output:






Polymorphism

Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Polymorphism is one of the most important concept of object oriented programming language. The most common use of polymorphism in object-oriented programming occurs when a parent class reference is used to refer to a child class object. Here we will see how to represent any function in many types and many forms.


Read More:Polymorphism



Benefits of OOP

  • OOP models complex things as reproducible, simple structures

  • Reusable, OOP objects can be used across programs

  • Allows for class-specific behavior through polymorphism

  • Easier to debug, classes often contain all applicable information to them

  • Secure, protects information through encapsulation




The Tech Platform

0 comments

Comments


bottom of page