Basic Principles of Object-Oriented Programming

23 oktober 2020 om 10:00 by ParTech Media - Post a comment

‘What is object-oriented programming’ - Pop this question to any developer and nine out of ten times, they will get the definition right. OOP is the creation of objects that has both data and functions. Ask the same developer to explain the concept in a simple language, they might struggle.

The basic principles of OOP involves Abstraction, Encapsulation, Inheritance, and Polymorphism. There are also objects and classes. Together, they stand as the working principle of any object-oriented programming language.

In this post, we have covered all these basic principles of OOP in a jargon-less format. This post has also covered some real-life examples and sample programs to help you understand the different OOPs concepts without any difficulties.

Table of contents

  1. What is an Object?
  2. What are Classes?
  3. What is abstraction?
  4. What is encapsulation?
  5. What is inheritance?
  6. What is polymorphism?
  7. Final Words

What is an Object?

Let us understand this with an example. Consider your mobile phone as an object. There can be different properties for your mobile phone like its model, software version, and memory in it. This object can also have functions like switch on the camera, turn off Bluetooth, restart, etc. In simple words, each object contains data and instructions to act on that data.

What are ‘Classes’?

This is another important term in object-oriented programming. A class is like a template from which new objects are created. Any class you create will always have a head and a body. A head typically includes modifiers and the keyword of the class while the body includes data members and member functions.

Here are the different components of a class –

  1. Public - The class members can be accessed from everywhere.
  2. Private - The class members can only be accessed by the defining class
  3. Protected - the class members can only be accessed by parent and inherited classes

A single program can contain any number of classes. Here’s an example of a program with a public class named Smartphone.

public class Smartphone {

  String make;

  int memorysize;

  String color;

  String software_version;
 

  void restart(){

​    System.out.println("Shutting Down");

  }

  void Flash(){

​    System.out.println("turning on flashlight");

  }

  void charge(){

​    System.out.println("charging");

  }}

What is Abstraction?

Do you ever drink Coffee? If you do, then there’s a good chance that you might have used a coffee machine in your life. The concept of abstraction is closely related to the principle of a coffee machine. You know that a coffee machine makes coffee, but you don’t need to know how it makes coffee. In short, all you need to know is about which buttons to press to make Coffee. You can use a button defined interface to make coffee, without needing to worry about the internal working of a machine.

The same principle goes into the concept of an Object-oriented programming language. In simple words, it is a concept that is not associated with any particular instance.

Consider your method as the coffee machine and your input parameter as the buttons on the machine. Abstraction allows you to create seamless programs by just knowing what method to call and what parameters to input. Here’s an example program to define Abstraction

#include <iostream> 

class Abstraction 

{ 

  private: 

​    int c, d; 

  public:  

​    void set(int x, int y) 

​    {  c = x; 

​      d = y; } 

​    void display() 

​    { 

​      cout<<"a = " <<a << endl; 

​      cout<<"b = " << b << endl; 

​    } 

}; 

 

int main() 

{ 

  Abstraction obj; ( Defining )

  obj.set(10, 20); 

  obj.display(); 

  return 0; 

} 

**Output:**

c = 10

d = 20

What is Encapsulation?

It is a group of properties and members under a single class or Object. Programs can be really long and there can easily be a ton of moving parts in it. After some time it gets really tough to maintain all these objects without them running into complexity.

This is where a primary principle like encapsulation comes into play. You can use this principle to encapsulate a set of objects into different classes. With this principle, you can prevent the repetition of code and also shorten the length of your code. You can look at encapsulation like a medicinal pill. All the medicine(Objects) are stored inside a pill (class) and you can consume it whenever needed.

What is Inheritance?

It is the ability to acquire the properties of existing classes and create new ones. Inheritance allows you to reuse code without having to rewrite it in a program. One of the best features of Inheritance is the ability to shorten the code in a program. You can use this principle to inherit codes from another class and reuse it in a new class.

What is Polymorphism?

Polymorphism refers to one name with many forms. It is the ability of one function to perform in different ways. In other words, it refers to an object’s ability to take on more than one single form.

Polymorphism can be applied in two simple ways

  1. Method Overloading
  2. Method Overriding

Method Overloading

When a class has multiple methods with the same names but a different set of parameters, it is called Method overloading. You can proceed with method overloading, only if your methods satisfy any one of the following rules -

  • Has different parameters
  • Has different return types
  • Has different access modifiers.

Here is an example of Method Overloading

class coding{

  public void code() {

​    System.out.println("Coding in C++");

  }

  public void code(String language) {

​    System.out.println("Coding in "+language);

  }

}

public class MethodOverloader {

  public static void main(String[] args) {

​    coding gorilla = new JavaProgrammer();

​    gorilla.code();

​    gorilla.code("Java");

  }

}

 

Output:

Coding in C++

Coding in Java

Method Overriding

The second way to go ahead with polymorphism is method overriding. This is only possible if a subclass ( or ) sister class has the same method as the parent class. Much like Method overloading, there are also some rules for method overriding to work.

  • Has the same parameter list
  • Has the same return type
  • And should have an access modifier that more restrictive than that of the parent class.

Here is an example of method overriding -

public class coding {

  public void code() {

​    System.out.println("Coding in C++");

  }

}

public class JavaProgrammer extends coding{

  public void code() {

​    System.out.println("Coding in Java");

  }

}

public class MethodOverridder {

  public static void main(String[] args) {

​    coding ben = new JavaProgrammer();

​    ben.code();

  }

}

 

Output:

Coding in Java

Final Words

By now you would have a better idea about the basic principles of OOP and how to explain it to others in simple language. Always remember - The 4 concepts discussed in this post will always stand at the forefront of any object-oriented programming language - no matter how complicated they might be.