Introduction
Object-oriented programming (OOPs) is a programming paradigm that represents real-world entities as software objects that have properties and methods to manipulate them. OOPs enables programmers to write code that is organized, reusable and easier to maintain. In this tutorial, we will explore the basics of object-oriented programming, covering key concepts like classes and objects, also we'll see some real world and programming world examples of OOPs. By the end of the tutorial, you will have a solid understanding of the fundamental concepts of OOPs.
Table of Contents :
- Object oriented programming System (OOPs)
- Procedural programming Vs OOPs
- Classes and Objects
- Real World example of classes and Objects
- Programming example of classes and Objects
Object oriented programming System (OOPs)
- Object oriented programming is a programming technique that combines
- data and
- the functions that process that data.
- Object oriented programming is usually abbreviated as OOPs.
- OOPs is a way of organizing code that is based on the concept of objects and classes.
- In OOPs, programming entities are treated as objects.
- Objects are entities that contain data as well as functions that operate on that data,
- This is the reason of naming it as object oriented programming.
Procedural programming Vs OOPs
Below are some differences between procedural programming and object oriented programming.
- Procedural programming is just about writing instructions to process data whereas OOPs is about packing data and instructions together.
- OOPs is faster than procedural programming.
- OOPs is easier to execute than procedural programming.
- OOPs programs are tidier and have an uncomplicated structure.
- OOPs code is highly reusable.
- OOPs code is easier to maintain.
- OOPs code is easier to debug.
- OOPs uses lesser code for more instructions.
- The development time in OOPs is much less than in procedural programming.
- OOPs code is much more extendible than procedural code.
Classes and Objects
- Object oriented programming has two main aspects : classes and objects.
- A Class in OOPs is a template that define
- the type of data that can be used
- the functions that will operate on that data
- An object is an instance of a class.
- The object of a class contains same type of data and functions that were defined in the class.
- There can be many instances of a class i.e. there can be many objects that belong to a single class.
- Each object will have same type of data and same type of functions but each object will have its own identity.
- The type of data will be the same in each object but the values of the data will be different, which makes each object different.
- We'll have a look at some real world and programming example of classes and objects in the next sections, to get a better understanding OOPs.
Real World example of classes and Objects :
Below we have a simple real world example of treating Fruits as a class and different fruits as the objects of this class. Lets study this real scenario point-wise :
- Fruits is a class
- Apple, banana, Guava, Mango are objects of Fruits class.
- The class can have data like :
- Price of fruit
- weight available
- shelf life
- Each object of the Fruits class will have some price but it will be different for all objects.
- Price of Apple can be 200/kg
- Price of banana can be 50/dozen.
- Each object of the Fruits class will have some weight available but it will be different for all objects.
- Weight available of Apple can be 100kg
- Weight available of Mango can be 50kg
- The class can have functions like :
- Peel the fruit
- Cut the fruit
- Serve the fruit
- These functions can be performed on each object of the fruit class.
Programming example of classes and Objects :
Below we have a simple programming world example of a class Employee
and different employees as the objects of this class. Let's study this programming world scenario point-wise :
Employee
is a class.- It is a template that defines
- The state of each employee i.e. the data associated with each employee.
- The behavior of each employee i.e. the functions that each employee can perform.
- Suppose there are four employees namely Steve, John, Christa, Stephen.
- Hence
Steve
,John
,Christa
,Stephen
are instances of theEmployee
class i.e. these four are objects. - The state of the objects is defined by the data of the class.
- The
Employee
class has following data :
Employee :
Name of the Employee
Employee Id
Designation
experience
email-Id
Address
Attendance
Perks
- Each employee (i.e. each object of the
Employee
class) will have some name, id, designation etc. but these will be different for each employee. - The behavior of the class is defined by functions like :
- The
Employee
class has following functions :
Employee :
Record attendance
Calculate salary
Calculate PF
Assign new work
Communicate
- These functions can be performed with each employee i.e. on each object of the
Employee
class.
Creating objects using the Employee class
Using the Employee
class discussed above we can create different objects for each employee like :
Object 1 :
State :
Name : Steve
Emp. Id : 2301
Designation : Project Manager
Behavior :
Record Attendance - Steve was present for 90% of the days this year.
Calculate salary - The basic salary is calculated with a basic salary of 15 LPA
Object 2 :
State :
Name : Rob
Emp Id : 1987
Designation : Lead Engineer
Behavior :
Record Attendance - Steve was present for 81% of the days this year.
Calculate salary - The basic salary is calculated with a basic salary of 10 LPA
Prev. Tutorial : String Methods
Next Tutorial : OOPS concepts