Professional Documents
Culture Documents
Lab # 02 Introduction To Object Oriented Programming (Introduction To OOP) Objective
Lab # 02 Introduction To Object Oriented Programming (Introduction To OOP) Objective
Lab # 02 Introduction To Object Oriented Programming (Introduction To OOP) Objective
Objective
To get introduced with fundamentals of OOP.
Introduction of class and objects.
Theory
Introduction of OOP
For instance, an object could represent a person with a name property, age, address, etc., with
behaviors like walking, talking, breathing, and running. Or an email with properties like recipient
list, subject, body, etc., and behaviors like adding attachments and sending.
Put another way, object-oriented programming is an approach for modeling concrete, real-world
things like cars as well as relations between things like companies and employees, students and
teachers, etc. OOP models real-world entities as software objects, which have some data
associated with them and can perform certain functions.
The key takeaway is that objects are at the center of the object-oriented programming paradigm,
not only representing the data, as in procedural programming, but in the overall structure of the
program as well.
Page | 1
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
NOTE: Since Python is a multi-paradigm programming language, you can choose the paradigm
that best suits the problem at hand, mix different paradigms in one program, and/or switch from
one paradigm to another as your program evolves.
OOP Pillars
Class
Object
Method
Inheritance
Polymorphism
Data Abstraction
Encapsulation
Classes
Classes are used to create new user-defined data structures that contain arbitrary information
about something. In the case of an animal, we could create an Animal() class to track properties
about the Animal like the name and age.
It’s important to note that a class just provides structure—it’s a blueprint for how something
should be defined, but it doesn’t actually provide any real content itself. The Animal() class may
specify that the name and age are necessary for defining an animal, but it will not actually state
what a specific animal’s name or age is.
It may help to think of a class as an idea for how something should be defined.Focusing first on
the data, each thing or object is an instance of some class.
The primitive data structures available in Python, like numbers, strings, and lists are designed to
represent simple things like the cost of something, the name of a poem, and your favorite colors,
respectively.
Page | 2
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
For example, let’s say you wanted to track a number of different animals. If you used a list, the
first element could be the animal’s name while the second element could represent its age.
How would you know which element is supposed to be which? What if you had 100 different
animals? Are you certain each animal has both a name and an age, and so forth? What if you
wanted to add other properties to these animals? This lacks organization, and it’s the exact need
for classes.
Object:
While the class is the blueprint, an instance is a copy of the class with actual values, literally an
object belonging to a specific class. It’s not an idea anymore; it’s an actual animal, like a dog
named Roger who’s eight years old.
Put another way, a class is like a form or questionnaire. It defines the needed information. After
you fill out the form, your specific copy is an instance of the class; it contains actual information
relevant to you. You can fill out multiple copies to create many different instances, but without
the form as a guide, you would be lost, not knowing what information is required. Thus, before
you can create individual instances of an object, we must first specify what is needed by defining
a class.
Like other general purpose languages, python is also an object-oriented language since its
beginning. Python is an object-oriented programming language. It allows us to develop
applications using an Object Oriented approach. In Python, we can easily create and use classes
and objects.
Method
The method is a function that is associated with an object. In Python, a method is not unique to
class instances. Any object type can have methods.
Inheritance
Inheritance is the most important aspect of object-oriented programming which simulates the real
world concept of inheritance. It specifies that the child object acquires all the properties and
behaviors of the parent object.
Page | 3
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
By using inheritance, we can create a class which uses all the properties and behavior of another
class. The new class is known as a derived class or child class, and the one whose properties are
acquired is known as a base class or parent class.
Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means many and Morphs means
form, shape. By polymorphism, we understand that one task can be performed in different ways.
For example You have a class animal, and all animals speak. But they speak differently. Here,
the "speak" behavior is polymorphic in the sense and depends on the animal. So, the abstract
"animal" concept does not actually "speak", but specific animals (like dogs and cats) have a
concrete implementation of the action "speak".
Encapsulation
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly synonym
because data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting something
means to give names to things so that the name captures the core of what a function or a whole
program does.
Page | 4
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
2. It makes the development and maintenance In procedural programming, It is not easy to
easier. maintain the codes when the project
becomes lengthy.
3. It simulates the real world entity. So real-world It doesn't simulate the real world. It works on
problems can be easily solved through oops. step by step instructions divided into small
parts called functions.
4. It provides data hiding. So it is more secure than Procedural language doesn't provide any
procedural languages. You cannot access proper way for data binding, so it is less
private data from anywhere. secure.
5. Example of object-oriented programming Example of procedural languages are: C,
languages is C++, Java, .Net, Python, C#, etc. Fortran, Pascal, VB etc.
As we have already discussed, a class is a virtual entity and can be seen as a blueprint of an
object. The class came into existence when it instantiated. Let's understand it by an example.
Suppose a class is a prototype of a building. A building contains all the details about the floor,
doors, windows, etc. we can make as many buildings as we want, based on these details. Hence,
the building can be seen as a class, and we can create as many objects of this class.
On the other hand, the object is the instance of a class. The process of creating an object can be
called as instantiation.
In python, a class can be created by using the keyword class followed by the class name. The
syntax to create a class is given below.
Syntax
Page | 5
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Example # 1:
Output:
Page | 6
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Methods
Once there are attributes that “belong” to the class, you can define functions that will access the
class attribute. These functions are called methods. When you define methods, you will need to
always provide the first argument to the method with a self keyword.
Example # 2:
Output:
Page | 7
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
There are also special attributes in it that begins with double underscores (__). For example,
__doc__ gives us the docstring of that class.
As soon as we define a class, a new class object is created with the same name. This class object
allows us to access the different attributes as well as to instantiate new objects of that class.
We saw that the class object could be used to access different attributes.
It can also be used to create new object instances (instantiation) of that class. The procedure to
create an object is similar to a function call.
1. >>> ob = MyClass()
This will create a new instance object named ob. We can access attributes of objects using the
object name prefix.
Attributes may be data or method. Method of an object are corresponding functions of that class.
Any function object that is a class attribute defines a method for objects of that class.
Page | 8
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Example # 3
Output:
Page | 9
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Lab Task
2.1 write a program to create a class in which find out the area of trapezoid.
2.2 write a program to create a class in which you create the details of patient records and display it.
2.3 write a program to create a class in which you convert rupees into dollars.
NOTE: Attach printouts of above mentioned task with your name and roll number in header or
footer.
Learning outcomes:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Page | 10