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. 

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++. 


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. 


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. 


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


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 PG Diploma 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.

Become a Full Stack Developer


Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses