Introduction of Object-Oriented Programming
Object-Oriented Programming or OOPs refers to languages that use objects in programming.
Object-oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc in programming. The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the code can access this data except that
function.
OOPs Concepts/ Characteristics:
Class
Objects
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
1. Class:
A class is a user-defined data type. It consists of data members and member functions, which
can be accessed and used by creating an instance of that class. It represents the set of
properties or methods that are common to all objects of one type. A class is like a blueprint for
an object.
For Example: Consider the Class of Cars. There may be many cars with different names and
brands but all of them will share some common properties like all of them will have 4 wheels,
Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage
are their properties.
2. Object:
It is a basic unit of Object-Oriented Programming and represents the real-life entities. An
Object is an instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated. An object has an identity, state, and
behaviour. Each object contains data and code to manipulate the data. Objects can interact
without having to know details of each other’s data or code, it is sufficient to know the type of
message accepted and type of response returned by the objects.
For example: “Dog” is a real-life Object, which has some characteristics like color, Breed,
Bark, Sleep, and Eats.
3. Data Abstraction:
Data abstraction is one of the most essential and important features of object-oriented
programming. Data abstraction refers to providing only essential information about the data to
the outside world, hiding the background details or implementation. Consider a real-life
example of a man driving a car. The man only knows that pressing the accelerators will
increase the speed of the car or applying brakes will stop the car, but he does not know about
how on pressing the accelerator the speed is increasing, he does not know about the inner
mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is
what abstraction is.
4. Encapsulation:
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism
that binds together code and the data it manipulates. In Encapsulation, the variables or data of
a class are hidden from any other class and can be accessed only through any member function
of their class in which they are declared. As in encapsulation, the data in a class is hidden from
other classes, so it is also known as data-hiding.
Consider a real-life example of encapsulation, in a company, there are different sections like
the accounts section, finance section, sales section, etc. The finance section handles all the
financial transactions and keeps records of all the data related to finance. Similarly, the sales
section handles all the sales-related activities and keeps records of all the sales. Now there
may arise a situation when for some reason an official from the finance section needs all the
data about sales in a particular month. In this case, he is not allowed to directly access the data
of the sales section. He will first have to contact some other officer in the sales section and
then request him to give the particular data. This is what encapsulation is. Here the data of the
sales section and the employees that can manipulate them are wrapped under a single name
“sales section”.
5. Inheritance:
Inheritance is an important pillar of OOP (Object-Oriented Programming). The capability of a
class to derive properties and characteristics from another class is called Inheritance. When we
write a class, we inherit properties from other classes. So, when we create a class, we do not
need to write all the properties and functions again and again, as these can be inherited from
another class that possesses it. Inheritance allows the user to reuse the code whenever possible
and reduce its redundancy.
6. Polymorphism:
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. For example,
A person at the same time can have different characteristics. Like a man at the same time is a
father, a husband, an employee. So, the same person possess different behaviour in different
situations. This is called polymorphism.
7. Dynamic Binding:
Dynamic binding, also known as late binding, is a concept in Object-Oriented Programming
(OOP) where the method to be invoked is determined at runtime rather than at compile time.
This allows for more flexible and extensible code, as the exact method that will be executed is
not known until the program is running. Dynamic binding is primarily used in the context
of polymorphism and inheritance. It allows a program to decide which method to call based on
the actual object type, rather than the reference type. This is achieved through the use
of virtual functions in languages like C++ and overridden methods in languages like Java.
8. Message Passing:
It is a form of communication used in object-oriented programming as well as parallel
programming. Objects communicate with one another by sending and receiving information to
each other. A message for an object is a request for execution of a procedure and therefore will
invoke a function in the receiving object that generates the desired results. Message passing
involves specifying the name of the object, the name of the function, and the information to be
sent.
Why do we need object-oriented programming
To make the development and maintenance of projects more effortless.
To provide the feature of data hiding that is good for security concerns.
We can solve real-world problems if we are using object-oriented programming.
It ensures code reusability.
It lets us write generic code: which will work with a range of data, so we don’t have to
write basic stuff over and over again.
Object oriented languages
The following is a quick overview of the Top 5 Object-Oriented Programming Languages
each developer must know. It includes Python, Java, C, C#, Ruby, and so on.
1. Python
one of the advanced languages in programming.
high-level programming language most commonly used by programmers.
used in building applications, automation, analysis of data, etc.
easy to learn.
syntax is simple and indentations play a key role.
got a lot of built-in packages, libraries, and plugins that help programmers in the
development or analysis process.
The libraries also enable Python for machine learning, web processing, etc.
Data processing, visualizing, etc. are possible through data-focused libraries
2. Java
Java Programming Language is class and object-based.
most commonly used programming language in developing web applications.
All of the OOPs concepts like encapsulation, abstraction, inheritance, and
polymorphism form the basics of Java programming language.
high-level, platform-independent, free, easy to learn, secure, and fast.
The code is structured and could be reused.
3. C++
language that supports object-oriented programming paradigms.
advanced version of C.
Java is said to be influenced by C++.
Browsers, software applications, operating systems, data structures, etc. can be
developed.
It provides different types of memory management.
4. JavaScript
The widely used language in the World Wide Web
It is easy to learn and enables the creation of dynamic websites, control
multimedia, animate objects, etc.
It is a client-side scripting language.
It allows the user to store and retrieve data
JavaScript lets the user interact with web pages, say data forms, button
functions, hovering text, and so on.
5. C#
C# belongs to the family of C.
C# is often used in the .net platforms to develop professional, dynamic websites.
It is also commonly used by game developers.
C# is comparatively faster than Java.
High scalability and the development of projects at a faster pace are the major
highlights.
Procedural Programming vs Object-Oriented Programming
Below are some of the differences between procedural and object-oriented programming:
Procedural Oriented Programming Object-Oriented Programming
In object-oriented programming, the
In procedural programming, the program is
program is divided into small parts
divided into small parts called functions.
called objects.
Procedural programming follows a top-down Object-oriented programming follows
approach. a bottom-up approach.
There is no access specifier in procedural Object-oriented programming has access
programming. specifiers like private, public, protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any Object-oriented programming provides data
proper way of hiding data so it is less secure. hiding so it is more secure.
In procedural programming, overloading is Overloading is possible in object-oriented
not possible. programming.
In procedural programming, there is no In object-oriented programming, the concept
concept of data hiding and inheritance. of data hiding and inheritance is used.
In procedural programming, the function is In object-oriented programming, data is
more important than the data. more important than function.
Procedural programming is based on Object-oriented programming is based on
the unreal world. the real world.
Procedural programming is used for Object-oriented programming is used for
designing medium-sized programs. designing large and complex programs.
Procedural Oriented Programming Object-Oriented Programming
Procedural programming uses the concept of Object-oriented programming uses the
procedure abstraction. concept of data abstraction.
Code reusability absent in procedural Code reusability present in object-oriented
programming, programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Basic Principles of OOPS (Object-Oriented Programming)
OOP – Object-Oriented Programming Principle is the strategy or style of developing
applications based on objects.
Anything in the world can be defined as an object. And in the OOPs, it can be defined in terms
of its properties and behavior.
For Example – Consider a Television, It is an object. And the properties related to it are. It’s
With and Height, color, type (SmartTV or CRT TV), etc. And the behavior can be – we can
change the channel, Adjust volumes and settings, switch off, switch on, etc are lots of behavior
are there. The operation on the behavior will affect the properties. Changing the channel will
affect its property.
So, similar things happen in Object-Oriented programming also, that each object has some
properties and behavior associated with it, that are defined in the class.
In short, it is a programming methodology that has certain principles and with the help of
these principles, the software is developed.
There are many other methodologies used for software development, But the most popular is
Object-Oriented programming.
Why OOPs are Popular in Comparison to Other Types of Programming Principles?
Object-Oriented Programming Principles is the most popular amongst others because it
relates to real-life objects. Every operation that is going to be functional is considered in
terms of classes and objects. That provides a better programming style because you need not
write code that needs to run anytime. Instead, you can create classes defining the functionality
and call that function by creating an object of that.
Other than these, it also provides principles like inheritance, that increase the code reusability
and help in applying updates very easily. And Abstraction, Encapsulation helps in enhancing
the security of the data.
Languages that support Object-Oriented Programming are – C++, Java, JavaScript, C#,
PHP, etc.
And Languages that are completely Object-Oriented Programming or Pure Object-Oriented
are – python, Ruby, Scala, etc.
Class And Object
Class is a detailed description, the definition, and the template of what an object will be. But it
is not the object itself. Also, what we call, a class is the building block that leads to Object-
Oriented Programming. It is a user-defined data type, that holds its own data members and
member functions, which can be accessed and used by creating an instance of that class. It is
the blueprint of any object. Once we have written a class and defined it, we can use it to create
as many objects based on that class as we want. In Java, the class contains fields, constructors,
and methods. For example, consider the Class of Accounts. There may be many accounts with
different names and types, but all of them will share some common properties, as all of them
will have some common attributes like balance, account holder name, etc. So here,
the Account is the class.
Object is an instance of a class. All data members and member functions of the class can be
accessed with the help of objects. When a class is defined, no memory is allocated, but
memory is allocated when it is instantiated (i.e. an object is created). For Example,
considering the objects for the class Account are SBI Account, ICICI account, etc.
Difference Between Class And Object:
There are many differences between object and class. Some differences between object and
class are given below:
Class Object
Class is used as a template for declaring and
An object is an instance of a class.
creating the objects.
When a class is created, no memory is Objects are allocated memory space
allocated. whenever they are created.
The class has to be declared first and only An object is created many times as per
once. requirement.
Class Object
A class can not be manipulated as they are not
Objects can be manipulated.
available in the memory.
A class is a logical entity. An object is a physical entity.
It is created with a class name in C++ and
It is declared with the class keyword
with the new keywords in Java.
Class does not contain any values which Each object has its own values, which are
can be associated with the field. associated with it.
A class is used to bind data as well as methods
Objects are like a variable of the class.
together as a single unit.
Example: Bike Example: Ducati, Suzuki, Kawasaki
Abstraction
Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its
main goal is to handle complexity by hiding unnecessary details from the user. That enables
the user to implement more complex logic on top of the provided abstraction without
understanding or even thinking about all the hidden complexity.
Abstraction in the real world
Making coffee with a coffee machine is a good example of abstraction.
You need to know how to use your coffee machine to make coffee. You need to provide water
and coffee beans, switch it on and select the kind of coffee you want to get.
The thing you don’t need to know is how the coffee machine is working internally to brew a
fresh cup of delicious coffee. You don’t need to know the ideal temperature of the water or the
amount of ground coffee you need to use.
Someone else worried about that and created a coffee machine that now acts as an abstraction
and hides all these details. You just interact with a simple interface that doesn’t require any
knowledge about the internal implementation.
You can use the same concept in object-oriented programming languages like Java.
Abstraction in OOP
Objects in an OOP language provide an abstraction that hides the internal implementation
details. Similar to the coffee machine in your kitchen, you just need to know which methods
of the object are available to call and which input parameters are needed to trigger a specific
operation. But you don’t need to understand how this method is implemented and which kinds
of actions it has to perform to create the expected result.
Different Types of Abstraction
There are primarily two types of abstraction implemented in OOPs. One is data abstraction
which pertains to abstracting data entities. The second one is process abstraction which hides
the underlying implementation of a process.
Data Abstraction
Data abstraction is the simplest form of abstraction. When working with OOPS, you primarily
work on manipulating and dealing with complex objects. This object represents some data but
the underlying characteristics or structure of that data is actually hidden from you. Let’s go
back to our example of making coffee.
Let’s say that I need a special hazelnut coffee this time. Luckily, there’s a new type of coffee
powder or processed coffee beans that already have hazelnut in it. So I can directly add the
hazelnut coffee beans and the coffee machine treats it as just any other regular coffee bean. In
this case, the hazelnut coffee bean itself is an abstraction of the original data, the raw coffee
beans. I can use the hazelnut coffee beans directly without worrying about how the original
coffee beans were made to add the hazelnut flavour to it.
Therefore, data abstraction refers to hiding the original data entity via a data structure that can
internally work through the hidden data entities. As programmers, we don’t need to know
what the underlying entity is, how it looks etc.
Process Abstraction
Where data abstraction works with data, process abstraction does the same job but with
processes. In process abstraction, the underlying implementation details of a process are
hidden. We work with abstracted processes that under the hood use hidden processes to
execute an action.
let’s say our coffee machine has a function to internally clean the entire empty machine for us.
This is a process that we may want to do every once a week or two so that our coffee machine
stays clean. We press a button on the machine which sends it a command to internally clean it.
Under the hood, there is a lot that will happen now. The coffee machine will need to clean the
piston, the outlets or nozzles from which it pours the coffee, and the container for the beans,
and then finally rinse out the water and dry out the system.
A single process of cleaning the coffee machine was known to us, but internally it implements
multiple other processes that were actually abstracted from us. This is process abstraction in a
nutshell.
Encapsulation
Object-Oriented Programming (OOP) is commonly used to develop complex software
systems. It is a programming paradigm based on the concept of “objects” that provides an
organized approach to solving problems by breaking down a system into smaller, more
manageable objects. These objects can represent real-world entities or abstract concepts.
Encapsulation is a key concept in OOP. This concept involves combining data and the
methods that operate on it into one unit, usually a class. Encapsulation protects data from
accidental modification, enhances code organization, and streamlines interaction between
program components.
Encapsulation is a fundamental concept in OOP that combines data (attributes) and methods
that work with that data into a single unit known as a class. This protective layer around the
data maintains its integrity and prevents unauthorized access.
Consider a car. The car body houses the engine, transmission, and brakes. You can interact
with the car using its controls (steering wheel, accelerator, and brake pedal), but you don’t
need to understand how they function. This is similar to the way encapsulation works in
programming.
How Encapsulation Works
To achieve encapsulation, you typically:
1. Declare data members as private: This restricts direct access to the data from outside the
class.
2. Provide public methods (getters and setters): These methods act as intermediaries,
allowing controlled access to the data. Getters retrieve data values, while setters modify
them.
3. Implement data validation: Within the setter methods, you can validate data before
assigning it to the object’s properties, ensuring data integrity.
The Benefits of Encapsulation
Data Protection: By making data private, you prevent accidental or intentional
modification from outside the class, ensuring data integrity.
Increased Security: Encapsulation helps protect sensitive information by restricting
access to it.
Code Reusability: Encapsulated classes often boast greater reusability because they
function as self-contained units with clearly defined interfaces.
Improved Maintainability: Changes to the internal implementation of a class are less
likely to affect other parts of the program due to encapsulation.
Key Components of Encapsulation
Data Hiding
Data hiding is the fundamental principle of encapsulation. It involves restricting direct access
to an object’s internal state. By making data members (attributes) private, you prevent external
code from directly modifying them. This protection ensures data integrity and prevents
unintended consequences.
Why is data hiding important?
Data Integrity: By controlling access to data, you can ensure that it remains consistent
and valid.
Code Maintainability: Isolating data within a class makes code easier to understand,
modify, and test.
Security: Protecting sensitive data from unauthorized access is crucial in many
applications.
Data hiding in Java is achieved by using the “private” access modifier. This prevents direct
access to class members (attributes and methods) from outside the class.
Access Modifiers
These keywords determine the accessibility of classes, methods, and other members. They are
part of the programming language syntax that allows for component encapsulation. Types of
access modifiers include:
Default
Private
Protected
Public
Default
Omitting an access modifier for a class, method, or data member grants access only within the
current package.
If you omit the access modifier (private, protected, or public), Java applies the default access
modifier.
Private
The private keyword declares members accessible only within the declaring class. Other
classes in the same program cannot access these private methods or data members. Classes
and interfaces cannot be private.
Public
The public access modifier is declared using the keyword public. It has the widest scope
among all other access modifiers. Classes and methods that are declared public can be
accessed from anywhere in your code. There are no restrictions on public data members.
Inheritance
There are types of inheritance based on the way the child has inherited from its parent. Here
are the types…
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance:
we can see there is one father Class and one child class that inherits from the father.
Multilevel Inheritance:
Here there are not only child and parent classes but also grandparent classes. Meaning the “C”
class inherits from “B”. And “B” class inherits from the “A” class. A is the Grandfather, B is
the Father and C is the child class.
Thus we can also have this chain of thousands of generation of inheritance.
Multiple Inheritance:
In this case there are more than one parent of the child class. This means, the child class will
inherit from all of its parents.
the class “C” inherits from class “B” and class “A” . And here both “B” and “A” class are the
parent classes of class “C”. This means any object of “C” class will be able to access both the
“properties” of “A” and “B” class.
Hierarchical Inheritance:
Here, we find more than one child of a parent class. Let’s say there is a class “A” as the parent
class. “B” class inherits from class “A”. There is another class named “C” that also inherits
from class “A”. meaning its like you and your sibling. You are class “B” and your sibling is
class “C” and both of you inherit from your father class “A”.
from the diagram we can note that, there is a father class A and two child classes of the father
class. The child classes both inherit from the father class “A”.
Hybrid Inheritance:
Hybrid inheritance is a bit different than others. you can say that it is a cocktail
of Heirarchical and Multiple inheritance.
Here class “A” is Grandparent and there are two other classes derived from the Grandparent.
These classes are, class “B” and class “C”, which are parent classes. Then there is the child
class “D” who inherits from class “B” and class “C”.
Polymorphism
Polymorphism is one of the core concepts of object-oriented programming (OOP) that
describes situations in which something occurs in several different forms. In computer
science, polymorphism describes the concept that you can access objects of different types
through the same interface. Each type can provide its own independent implementation of this
interface.
Different Types of Polymorphism
Java supports 2 types of polymorphism:
-static or compile-time
-dynamic or run time
Static Polymorphism
Java allows you to implement multiple methods within the same class that use the same name.
But Java uses a different set of parameters called method overloading and represents a static
form of polymorphism.
Dynamic Polymorphism
This form of polymorphism doesn’t allow the compiler to determine the executed method.
The JVM needs to do that at runtime.
Within an inheritance hierarchy, a subclass can override a method of its superclass, enabling
the developer of the subclass to customize or completely replace the behavior of that method.
Doing so also creates a form of polymorphism. Both methods implemented by the super- and
subclasses share the same name and parameters. However, they provide different functionality.
Modularity
The module simply means the software components that are been created by dividing the
software. The software is divided into various components that work together to form a single
functioning item but sometimes they can perform as a complete function if not connected with
each other. This process of creating software modules is known as Modularity in software
engineering. It simply measures the degree to which these components are made up than can
be combined. Some of the projects or software designs are very complex that it’s not easy to
understand its working and functioning. In such cases, modularity is a key weapon that helps
in reducing the complexity of such software or projects. The basic principle of Modularity is
that “Systems should be built from cohesive, loosely coupled components (modules)” which
means s system should be made up of different components that are united and work together
in an efficient way and such components have a well-defined function. To define a modular
system, several properties or criteria are there under which we can evaluate a design method
while considering its abilities.
Example
Modularity refers to an organizing structure in which different components of a software
system are divided into separate functional units.
For example, a house or apartment can be viewed as consisting of several interacting units;
electrical, heating, cooling, plumbing, structure, etc. Rather than viewing it as one giant
jumble of wires, vents, pipes, and boards, the organized architect designing a house or
apartment will view them as separate modules that interact in well-defined ways. In doing so,
he/she is using the concept of modularity to bring clarity of thought that provides a natural
way of organizing functions into distinct manageable units. Likewise, using modularity in a
software system can also provide a powerful organizing framework that brings clarity to an
implementation.
Features of Object Orientation
Object-oriented programming (OOP) models software around objects, which represent real-
world entities. Each object combines data (attributes and state) and behavior (operations
and reactions). The five key features of OOP are: Attributes, State, Identity, Operations,
and Behaviour.
1. Attributes
Attributes (also called fields or properties) are the data elements that define the
characteristics of an object. They help describe what the object has.
Stored as variables inside the object.
Each object has its own set of attribute values.
Example (Car object):
Attributes:
- make = "Toyota"
- model = "Corolla"
- color = "Red"
- speed = 60
These define the static details of the car.
2. State
The state of an object is the current condition or situation of that object, defined by the
current values of its attributes at a given time.
Changes over time as the object interacts with operations.
Critical for defining object behavior.
Example:
State:
- color = "Red"
- speed = 60
- engineStatus = "On"
Changing speed from 60 to 0 changes the object’s state
3. Identity
Identity is what makes each object unique, even if other objects have the exact same attributes
and state.
Each object is a separate instance in memory.
Typically represented by a memory address or unique identifier.
Example:
Car A and Car B both:
- make = "Toyota"
- model = "Corolla"
- color = "Red"
Even if attributes and state are identical, they are still two different cars with separate
identities.
4. Operations
Operations (also called methods or functions) define the behavioral capabilities of an object
— what the object can do or what can be done to it.
Declared in the class.
Interact with or modify attributes.
Example (Car methods):
Operations:
- start()
- accelerate()
- brake()
- changeGear()
Calling accelerate() might increase the car’s speed — altering its state.
5. Behaviour
Behaviour is the observable actions and reactions of an object, driven by operations and
influenced by its state.
Behaviour results from invoking operations based on the object’s current state.
Determines how the object responds in different situations.
Example:
Calling accelerate():
If engineStatus = "On" and speed < maxSpeed → speed increases.
If engineStatus = "Off" → nothing happens.
The same method triggers different behaviours based on the object's state.
Assignment works 1
Write the applications of oops concepts
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
5. Data hiding
6. Message passing