Object Oriented Programming Concepts

Isuri Punchihewa
7 min readMar 7, 2021

What is Object Oriented Programming??

Object Oriented Programming combines a group of attributes and methods in to a single unit which is known as an “object”. The primary purpose of OOP is to increase the flexibility and the maintainability of the programs. And combining the attributes and methods into a single unit makes it so much easier to understand how a program works.

Before beginning to talk about the OOP concepts, let’s get an idea about the classes and objects.

A Class…

“A class is a blueprint/template or a structure where we can define attributes and methods to utilize by objects”.

An Object…

“An object is an instance of a class which represent a real world entity.”

What are Object Oriented Concepts?

Object Oriented Programming has four fundamental concepts which increase the readability and the maintainability of the programs. They are…

  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction

Let’s talk about these concepts separately.

1. Inheritance

“Inheritance is a tightly coupled relationship between two or more classes where one class acquire all the attributes and the methods of another class.”

In this mechanism, the class which has acquired all the attributes and methods from another class is known as the “child class, the sub class or the derived class.

And the class, whose attributes and methods has been acquired by another class in known as the “parent class, the super class or the base class.

Inheritance is representing an IS-A relationship between the parent class and the child class.

When using this concept, the child class may have some extra attributes and methods which are not in the parent class. But it is necessary for the child class to inherit all the attributes and the methods which are in the parent class. This method support the concept of reusability of the program.

As inheritance is a tightly coupled relationship, making changes to the parent class risks breaking the code.

There are five types of inheritance

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid(Virtual) Inheritance

Let’s take an real world example.

This picture is consist of different birds such as parrots, eagles, owls and etc. All these birds are showing many common features. But still each and every bird has one or more unique features . Hence we can take a parent class as a “Bird” which has all the common attributes and functions. And we can take child classes as parrot, owl, eagle and etc. The child classes can acquire all the functionalities in the parent class by extending the bird class. This is called Inheritance.

2. Polymorphism

“Polymorphism refers to the ability of an object to take several forms.”

The best real life example for the polymorphism is “Mother”. A Mother can be a wife, a daughter, an employee at the same time. So the same person is possessing different behavior at the same time.

Likewise polymorphism is the capability of a method to behave in different forms depending on the object it is created on.

There are mainly two types of polymorphism.

  1. Compile-Time polymorphism (Static Binding)
  2. Runtime polymorphism (Dynamic Binding)

2.1. Compile-Time Polymorphism

Compile time Polymorphism is the kind of polymorphism which is resolved during the compile time. This mechanism is also known as the ‘Static Binding’.

Method Overloading is an example for the compile-time polymorphism.

Method Overloading

Method Overloading is a feature that allows a class to include two or more methods having the same name but with different parameters.

There are three ways where the parameters of the overloaded functions can be differ.

  1. Having Different Number of Parameters
public void average(float num1, float num2){
float avg;
avg = (num1 + num2) / 2;
System.out.println("Average is "+avg);
}
public void average( float num1, float num2, float num3){
float avg;
avg = (num1 + num2 +num3) / 3;
System.out.println("Average is " +avg);
}

In here, though the name of the two functions are the same, the number of parameters are different. Hence these two functions present two different method implementations.

2. Having Different Types of Parameters

public void sum(float num1, float num2){
float sum;
sum = num1 + num2;
System.out.println("Summation is "+sum);
}
public void sum(int num1, int num2){
int sum;
sum = num1 + num2;
System.out.println("Summation is "+sum);
}

In this, the name of the two functions and the number of parameters can be similar but the type of the parameters are different in each function.

3. Expecting Parameters in Different Orders

public void display(String name, int age, String grade){
System.out.println("Name : "+name+"/nAge : "+age+ "/nGrade :
+grade);
}
public void display(String name, String grade, int age){
System.out.println("Name : "+name+"/nGrade: "+grade+ "/nAge: "
+age);
}

Though the name, return type and the parameters are the same in these two functions, the order of expecting the parameters are different from each other.

2.2. Runtime Polymorphism

Runtime polymorphism is the process in which a call to an overridden method is resolved at runtime. This mechanism is also known as the ‘Dynamic Binding’.

Method Overriding is one of the ways to achieve runtime polymorphism.

Method Overriding

Method Overriding is a feature that allows a child class to provide specific implementation of a method that is already provided by its super class. The methods that are implemented by the parent and child classes, share the same name, parameters but provide different functionalities.

3. Encapsulation

“Encapsulation is the method of wrapping up attributes and functions that work on that data under a single unit.”

If you have an attribute which is not visible from the outside of an object, and still bundled with the methods which provides the read and write access to it, you can hide specific information and control access to the state of the object.

This concept is often used to hide the internal representation or the state of the object from the outside. Hence this is also known as ‘Information Hiding’.

When we consider Java, there are four access modifiers which we can use to control read write access.

  1. ‘Private’ Modifier

Private modifier only allow access within the same class. If we have used the modifier as private with an attribute or a method, subclasses or any other classes within the same or a different package can not access that particular attribute or the method.

2. ‘Protected’ Modifier

Attributes and methods which are using the protected access modifier, can be accessed within your class, by all classes within the same package, and by all subclasses within the same or other packages.

This modifier is mostly used when the internal methods are need to be called or overridden by subclasses.

3.’Public’ Modifier

Public modifier is the least restrictive modifier. The attributes and the functions which used this modifier can be accessed within the same class as well as by all the other classes.

4. Default Modifier/No Modifier

When we don’t provide an access modifier, those attributes and functions can be accessed within the same class as well as from the other classes which are in the same package.

Let’s simplify this concept with a real world example.

We will assume that a specific banking system is consisted with different sections like ‘Loan section’ which handles all the activities related to loans, a ‘Pawning section’ which handles all the activities related to pawning, a ‘Savings sections’ which handles all the activities related to savings and etc.. Now Let’s say, one day, under some reason, a situation arise, where an official from the savings section needs to get the information of a particular customer from the loan section. But in this particular system he does not have the access to the savings section. Therefore he will have to contact some other official from the savings section and request him for the particular information. This is called as Encapsulation.

4. Abstraction

“Abstraction is refers to the act of representing essential features without including the background details and explanations.”

The main goal of using abstraction is to handle the complexity by hiding unnecessary details from the user.

In java, abstraction is achieved by using abstract classes and interfaces.

Let’s clarify abstraction using a real world example.

A coffee machine is a single object. Whenever you are using a coffee machine to make coffee, all you need to do is to provide water, coffee beans, switch it on and select the kind of coffee you want to make. In other words, all you need to know, is the steps of using the coffee machine. Which means you can make a cup of coffee without knowing how the coffee is made by the machine or how much ground coffee will be needed to make one cup of coffee or the ideal temperature of the water to brew coffee. These internal structure of the coffee machine is unknown to the users. But not knowing these details never affect the usage of the coffee machine. This is simply the Abstraction.

That’s simply the basics of the object oriented programming concepts. I hope you all enjoy reading this blog and hope you find this useful. Thank You for reading and Have a nice day!

--

--