Object Oriented Programming Lab 3
Object Oriented Programming Lab 3
Lab No. 03
Lab 03 – Introduction to Simple Classes, Attributes and Methods
Objectives:
Because there are so many ways to provide argument values to init (), there is a vast array of
use cases for object creation. We take a look at several of them. We want to maximize clarity, so
we need to define an initialization that properly characterizes the problem domain.
Before we can get to the init () method, however, we need to take a look at the implicit class
hierarchy in Python, glancing, briefly, at the class named object. This will set the stage for
comparing default behavior with the different kinds of behavior we want from our own classes.
In this example, we take a look at different forms of initialization for simple objects (for example,
playing cards). After this, we can take a look at more complex objects, such as hands that involve
collections and players that involve strategies and states.
One of the popular approach to solve a programming problem is by creating objects. This is
known as Object-Oriented Programming (OOP).
attributes
behavior
Parrot is an object,
The concept of OOP in Python focuses on creating reusable code. This concept is also known as
DRY (Don't Repeat Yourself).
Class:
Class is a set or category of things having some property or attribute in common and differentiated
from others by kind, type, or quality.
A class in Python is a category or set of different elements grouped together that share one or more
similarities with one another, but yet distinct from other classes via type, quality and kind. In
technical terminology, we can define a class in Python as being a blueprint for individual objects
with same or exact behavior.
Object:
Object is one of instances of the class. Which can perform the functionalities which are defined in
the class
Self:
Self represents the instance of the class. By using the "self" keyword we can access the attributes
and methods of the class in python.
init__:
" init " is a reserved method in python classes. It is known as a constructor in object oriented
concepts. This method called when an object is created from the class and it allow the class to
The init method is similar to constructors in C++ and Java. Constructors are used to initialize
the object’s state. The task of constructors is to initialize (assign values) to the data members of the
class when an object of class is created. Like methods, a constructor also contains collection of
statements (i.e. instructions) that are executed at time of Object creation. It is run as soon as an
object of a class is instantiated. The method is useful to do any initialization you want to do with
your object.
Exercise1:
#class name
class Person:
self.name = name
self.age = age
print(person1.name,person1.age)
person2 = Person("ahmed", 9)
print(person2.name,person2.age)
Exercise2:
class Person:
p1 = Person("Ali", 36)
p1.description()
Exercise 3:
class Car(object):
"""
blueprint for car
"""
def Details(self):
print("Car Details ",self.model, self.color,
self.company,self.speed_limit)
#1st Object
maruthi_suzuki = Car("ertiga", "black", "suzuki", 60)
maruthi_suzuki.Details()
#2nd Object
audi = Car("A6", "red", "audi", 80)
audi.Details()
Task 1Create UML diagrams for all the exercises and Tasks
Task 2: Create class residential houses. Each house object has different parameters such as number
of location of the house, rooms, parking available or not. Price of the house.
Task 3: You are hired in a mobile company which produces multiple mobiles each year. Select
any mobile company create class, add some features and methods to operate the mobile and then
create some objects for your friends and check how it will wok