Programs

OOPS Concepts Explained for Beginners

The role of computer programs is to successfully model complex real-world requirements into computer-understandable instructions. To accomplish that, several programming paradigms work on different philosophies. These include Functional Programming, Procedural Programming, Object Oriented Programming, and more. These paradigms differ based on how they work and are therefore language-independent. 

In this article, we’ll look at Object Oriented Programming in depth. Before we get into the complexities of OOPS, here are some key OOPs concepts in Java that you should keep in mind, as they are the four pillars on which OOPS works: 

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

We’ll talk about these OOPs concepts in depth at a later stage in this article. But first, let’s understand what exactly is Object Oriented Programming. 

What is Object Oriented Programming?

Before Object Oriented Programming, Functional Programming was the dominant programming paradigm. Languages like C and COBOL followed this paradigm, so the programs written using these languages were a series of instructions. These used subroutines or procedures to keep the source codes modular and more readable. The Functional Programming paradigm focused more on logic than data, and the program would combine both the data and program. 

Programming languages, like C++, C#, Java, and more, moved on to the Object Oriented Programming approach after C. OOPs prioritized data overwriting simple logic-based instructions. In essence, an object is anything that you want to model in your program. It could be anything and everything. If it sounds vague right now, stick till the end, and you’ll understand exactly what we mean by this! 

What are Classes and Objects? 

Object Oriented Programming works with Classes and Objects. So, it’s important to understand what these two terms mean and how they are different from one another. Simply put, think of Classes as a blueprint that helps create Objects as and when you need them.

The role of Classes is to define various attributes and different behaviors. So, if we were to model a vehicle in our program, we would make a Class for the vehicle and keep attributes such as the number of wheels, color, model, age, and so on; whereas the behaviors would be like start, accelerate, brake, and so on. As you can see, the attributes and behavior we have defined are not specific to one vehicle. They essentially encompass the different behavior and attributes across different vehicles. That way, when you want to instantiate a new vehicle object, you can specify the attributes per the vehicle in question. In this way, Classes act as a perfect blueprint for creating new objects with the same attributes and behaviors.

So, here is what our Class Vehicle and its different attributes and behaviors will look like. Remember that we’ve used Java’s syntax to write the code, but the programming paradigm is language-neutral. 

public class Vehicle{

private string _color;

private int _model;

private string _makeYear;

private string _fuelType;

public void start(){ }

public void accelerate() {}

public void stop() {} 

}

Using the defined Class, we can create various objects with different attributes while having common behaviors. For instance: 

Object 1
Model Alto
Fuel CNG
Make 2018
Start()

Break()

Accelerate()

Object 2
Model WagonR
Fuel Petrol
Make 2017
Start()

Break()

Accelerate()

This way, Classes in Object Oriented Programming can help you simplify modeling real-world complex behaviors and systems. All the data, attributes, and functions are bound to the object of any class. This way, the need to keep any global data is eliminated. This is an important difference between Procedural Programming and Object Oriented Programming approaches. 

With the basics settled, let’s look at the four pillars of Object Oriented Programming in depth:

Get data science certification from the World’s top Universities. Learn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Abstraction

Abstraction is all about focusing on what is done instead of how the task is performed. The whole idea behind abstraction is to help build independent modules which can be made to interact with one another without having any other reliance on one another. This way, the maintenance of a program also becomes easier and more manageable. With Abstraction, OOPs try to represent only essential features without going into how those features are implemented or how the system’s internals work. 

The idea behind one of the most important OOPS concepts in Python is that programmers need to focus only on the things that matter to the current module directly. Modifying one module should not directly impact other independent modules. A programmer only needs knowledge of the current module and what it gives. There is not much need to understand the background workings of the module to accomplish what is required; therefore, that information is abstracted out for best results. 

If you take the idea of abstraction beyond programming, you will realize that abstraction, in essence, is everywhere. The objects we deal with daily are abstracted from us at various levels. For example, we don’t know how our car brake’s internals work, but we have been provided with a pedal to apply brakes while abstracting the mechanism behind the curtains. That way, you don’t need to worry about how it works; you just need to press the pedal. 

Encapsulation

The encapsulation concept is closely tied to the previous concept of abstraction. Encapsulation is, in essence, all about providing the solution to a problem without requiring the end user to understand the domain of the problem fully. For doing this, encapsulation binds all the data and behavior together as a single unit. That way, the end-user is prevented from knowing about the inner elements wherever abstraction is implemented. 

The user cannot access the data directly and uses exposed functions to access the data as and when needed. By hiding the object’s internals, the OOPS programming paradigm protects the integrity of the modules and doesn’t allow users to set the internal data into an inconsistent state. If not done, this could cause many problems in the long run. So, in this way, encapsulation is more about exposing complexity in a failproof manner and less about completely hiding complexity. 

Inheritance

Inheritance is another powerful feature of the Object Oriented Programming paradigm. The role of inheritance is to efficiently and quickly arrange and organize classes into a hierarchy to enable these classes to inherit behavior and attributes from classes that lie above in the hierarchy. In essence, inheritance can be understood as an “is a relationship”, which is very similar to how we talk about things in the real world. 

For instance, we say that “a parrot is a bird”. That means that a parrot is an object of the class bird, and so it inherits some attributes and behaviors from the broader class. However, that won’t be correct if we say that a bank is a bank account. That is how the hierarchy in inheritance works, and it lets you define behaviors and implementations that can be later specialized for specialized classes. Keep in mind that inheritance doesn’t work backward. So, the parent class will never inherit anything from the child class. However, child classes inherit both attributes and behaviors from parent classes, depending on the nature of those attributes and behaviors (public or private). Inheritance is a way to reuse your program and can help you make your entire source code shorter and much more understandable. But you must remember that you shouldn’t go around adding levels after levels of inheritance if it isn’t needed. 

Check our US - Data Science Programs

Polymorphism

Polymorphism is one of the many important OOPS concepts in Python and Java, which essentially takes care of many different possible implementations of any executable units and all the subtle differences that go on in the background without making the user aware of those changes. Polymorphism makes it easier for computer programs to be extended with specialized objects without any difficulty. 

For instance, if you wish to write a piece of text on paper – you could use a pen, a marker, a crayon, or even a pencil. All you need is something that can fit in your hand and help you make symbols as you press it against a paper. So, the act of writing will help you make symbols on paper, whereas what instrument you require depends on your choice. 

In this sense, inheritance is a way for the program to achieve polymorphism, wherein the custom implementation of the method overwrites the behavior defined in the inherited class. This process is also known as run-time polymorphism or method overriding. There is another form of polymorphism, also known as method overloading. In that, inheritance does not come into the picture at all. In method overloading, the method name is kept the same, whereas the arguments in the method differ based on what task is to be performed. 

In conclusion

With that, we come to the end of our discussion on OOPS concepts in Java. The things discussed so far set all the foundation you require to get started with Object Oriented Programming without worrying about anything. You must implement the discussion and try things hands-on; after all, programming is all about practicing and persevering. 

The knowledge of OOPS will help you in your data science journey, too, as you will be using languages like Python, which relies on the OOPS paradigm. Data Science is a booming field, and there is no dearth of opportunities for people with the right skills and knowledge. Check out our MS in Data Science by upGrad, in collaboration with the University of Arizona, and get a chance to learn from industry experts alongside a strong network of alumni that will help you throughout your career. 

What are the four concepts of OOPS?

The four core concepts of OOPS are Abstraction, Encapsulation, Inheritance, and Polymorphism.

How do classes and objects differ?

Classes are the blueprints of the phenomena that are to be modeled. Objects, on the other hand, are particular instances of classes that borrow behavior and attributes from the class they are instantiated from.

What language does OOPS work on?

OOPS is a programming philosophy that is language-neutral. You can work on OOPS using any modern-day programming language like C++, Java, Python, Ruby, etc.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

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

Our Best Data Science 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