OOP Concepts and Examples That Every Programmer Should Know

In this article, we will cover the basic concepts around Object-Oriented Programming and discuss the commonly used terms: Abstraction, Encapsulation, Inheritance, and Polymorphism.

To begin with, OOP is a programming methodology that requires the programmers to create objects and use them throughout the program within the functions that require such objects for their operation. The reason why the OOPs concept is used extensively in java is that it allows the reusability of code while maintaining security. 

Check out our free courses to get an edge over the competition.

Explore Our Software Development Free Courses

Before we talk about the four pillars of object-oriented programming let us familiarize ourselves with the generic terms that we hear very often when using any of the object-oriented languages: Java, python, C++. 

Check out upGrad: Full Stack Development Bootcamp (JS/MERN)


A class is a collection of objects that defines a set of properties that is common to all objects of a particular type. It can also be called a blueprint for creating objects. A class entails the following components:

Class name: The name given to a class starting with a capital alphabet.

Modifiers: Based on the functionality of the class modifiers can either be public, private or default.

Body: The class body contains all the codes on the objects present in the class. This could range from declaring any variables or creating any constructor or methods that contain the functioning of an object. 

Check out upGrad: Java Bootcamp

Explore our Popular Software Engineering Courses

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?



An object is defined as an instance of a class and contains real-life entities. For instance, for a class called Animals, Its objects will be a cat, dog, elephant et al. Each object has its own identity, attribute, and behaviour. The code below depicts the use of class, object, and method while programming in the java language. 


Methods are defined within a class and are used to perform a specific function. The method may or may not contain an input parameter. The code below depicts the use of class, object, and method while programming in the java language.

In the above code, Player is the name given to our class, whereas runs is a parameter passed in the method Batsman which returns the runs scored by him when called via an object called myobj. 

Access Modifiers

The access modifiers in Java defines the accessibility or extent of a method or constructor or the class. The four types of access modifiers are:

  1. Public: The code written within a class is accessible to other classes. 
  2. Private: The code written is only accessible within that specific class. 
  3. Default: The code written is accessible within the same package. 
  4. Protected: The code is accessible within a package and also through a subclass. In the absence of a child class, the code cannot be accessed.

Now let’s proceed and talk about the crux of object-oriented programming. 

In-Demand Software Development Skills


The term inheritance refers to inheriting the properties of one class to another. The properties refer to the attributes and methods of the parent class. The parent class is that class whose properties need to be inherited by other classes. The classes that inherit the properties of the parent class are called the child class or subclass. To inherit the properties of the parent class into the child class, a keyword called extends is used. 

In the above example, the Sponsor is the parent class with the owner being its attribute. We have created a subclass called Team that inherits the parent class- Sponsor. We have created an object of Team that can access the properties of the parent class. The output of the above code is:


As the name suggests- Polymorphism is the ability of a variable or a function to exist in multiple forms. Polymorphism allows the programmer to perform different tasks using the same variable or function. A real-life example of polymorphism would be- consider an open ground, now this ground can be used for playing sports.

Besides, it could also be used to organize weddings and concerts. Lastly, the same ground can be used for parking vehicles. From this, we can infer that a single variable can have multiple implementations depending upon its usage. 

The polymorphism we usually come across two terms namely- Method overloading and Method overriding.

In Method Overloading, a single method can be used in numerous ways and perform different functions. The methods will have the same name but different parameters can be used as input. 

In Method Overriding, the method of the parent class can be overridden by the child class. With this, the same method can perform differently when invoked by the parent class and by the child class. 

An example of the polymorphism is shown below:

In this example, using the same method we can perform multiple tasks. The same method Voice when used in Bird would output “Turr Turr” and when used with Duck will output “Quack Quack”. The snapshot of output is shown below-


Abstraction is the process of hiding certain data from the users and showing only the required information to them. For instance, while driving a car, we are not concerned about internal functions or mechanisms. What is shown to us are the speed at which the car is being driven and the litres of petrol available. All the other marginalized data are not displayed to the driver. 

The abstract keyword is used for methods and classes while performing abstraction. For an abstract class, we cannot create an object while the abstract method should not include a body. If any of the two rules are violated, the output will generate an error. 

Here, we have created an object of the subclass- Duck which is inherited from the main class- Bird. The output is shown below:


Encapsulation is the process of binding the code and the data together into a single unit. Here, the variables of a class are hidden from other classes (by using the keyword private) but can only be accessed through a member function. Setter and getter functions are used to access the private variables of a class that is abstract. 

Until now, we have covered anything and everything that is related to object-oriented programming using Java. Before we conclude, let us look at some of the advantages of the OOP concept.

  1. The code can easily be reused and therefore saves a lot of time and cost for the development of codes.
  2. It helps in designing the code in a well-structured manner so that any new programmer does not have to spend long hours to understand the code.
  3. Besides helping users in writing code efficiently, it makes sure that security is not compromised. 

Checkout: OOPs Interview Questions & Answers

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Read our Popular Articles related to Software Development


To conclude, in this blog we have covered the basic concepts of OOPs. These concepts are used extensively in industry and other applications. In order to become a proficient programmer, one should have a solid grasp of these concepts. In the upcoming blogs, we will uncover several other essential programming concepts that will help you develop expertise in this domain. 

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.]

What is a factorial?

A factorial is a mathematical operation that counts the product of a given number and all the numbers below it. A factorial is a product that indicates how many times a number is multiplied by one. For example, the factorial of 5 is 5x4x3x2x1, which is equal to 120. The factorial of 1 is 1 and the factorial of 0 is also 1. The program to find a factorial of a number is the most commonly asked interview question and is something that should be on the tips of your hand.

How to write a factorial recursion program?

Factorial of a number n is defined as n! = 1 × 2 × 3 × 4 × … × n. Factorial of 5 is 120. The factorial function is defined as factorial(5) = 120. A recursive function is a function which calls itself. This is an example of a factorial function in recursive style factorial(n) = n * factorial(n - 1). Writing a factorial recursion program is very simple and the code is very similar to the iterative version. To write the iterative version, we use a variable called n and increment it by one and multiply it with the variable called prod, which keeps track of the successive values of n. The output value is also stored in the variable called prod. In the recursive version, you still use a variable called n. However, you don’t need a variable to store the value of prod and you can directly return prod from the function.

What is recursion in programming?

The word recursion is derived from the Latin word recurrere, which means to return. In most of the programming languages, a function which calls itself is called recursion. This looping process continues until the base case is reached, which is not defined in any recursive functions. Recursion is an effective technique for solving a problem in a structured and organized way. It is a good programming strategy. For instance, the Fibonacci series problem, the factorial problem, etc. can be solved in both iterative as well as recursive manner.

Want to share this article?

Become a Full Stack Developer

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks