Object Oriented Programming in Python E-Box A Training Report
Object Oriented Programming in Python E-Box A Training Report
E-Box
A training report
Bachelor of Technology
(computer science and engineering)
Submitted to
LOVELY PROFESSIONALUNIVERSITY
PHAGWARA, PUNJAB
Submitted by:
1
S.NO TITLE PAGE
2
To whom so ever it may concern,
3
4
ACKNOWLEDGEMENT
I would like to express my deepest appreciation to all those who provided me the possibility
to complete this report. A special gratitude I give to my mentor whose contribution in
stimulating suggestions and encouragement helped me to coordinate my project especially in
writing this report. I express my thanks to my institution Lovely Professional University for
giving me an opportunity to learn this interesting topic. I also convey my regards to my
faculty assistance all through this training named “Object Oriented Programming”. Once
again I would like to thank all my supporters from the core of my heart.
5
Introduction to Object Oriented Programming:
• Attributes
• Behaviour
We can take anything which has a name, age, colour as its attributes and the things which it
does as behaviour.
6
Class:
We can think of class as a sketch of a animal with labels. It contains all the details about
name, colour, size etc. Based on this information we can study about that particular animal.
Then that particular animal is called the object.
Here we use the class keyword to define an empty class MyClass. From class, we construct
instances. An instance is a specific object created from a particular class.
Object:
An object (instance) is an instantiation of a class. When class is defined, only the description
for the object is defined. Therefore, no memory or storage is allocated.
7
#program for creating class and object in python
#output:
In the above program, we created a class with the name parrot. Then, we defineattributes.
The attributes are a characteristic of an object.
These attributes are defined inside the init method of the class. It is the initializer
method that is first run as soon as the object is created.
Then, we create instances of the Parrot class. Here, blu and woo are references (value) to our
new objects.
8
We can access the class attribute using class .species . Class attributes are the same for all
instances of a class. Similarly, we access the instance attributes using blu.name and
blu.age. However, instance attributes are different for every instance of a class.
Methods:
Methods are functions defined inside the body of a class. They are used to define the
behaviours of an object.
#output:
In the above program, we define two methods i.e., sing() and dance(). These are called
instance methods because they are called on an instance object i.e., blu.
9
Inheritance:
Inheritance is a way of creating a new class for using details of an existing class without
modifying it. The newly formed class is a derived class (or child class). Similarly, the
existing class is a base class (or parent class).
10
#output
In the above program, we created two classes i.e. Bird (parent class) and Penguin (child
class). The child class inherits the functions of parent class. We can see this fromthe
swim() method.
Again, the child class modified the behaviour of the parent class. We can see this from the
whoisthis() method. Furthermore, we extend the functions of the parent class, by creating a
new run() method.
Additionally, we use the super() function inside the init () method. This allows us to run the
init () method of the parent class inside the child class.
Encapsulation
Using OOP in Python, we can restrict access to methods and variables. This prevents data
from direct modification which is called encapsulation. In Python, we denote private
attributes using underscore as the prefix i.e., single _ ordouble
11
#data encapsulation in python.
#output
As shown, to change the value, we have to use a setter function i.e., setMaxprice() which
takes price as a parameter.
12
Polymorphism
Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data
types).
Suppose, we need to color a shape, there are multiple shape options (rectangle, square,
circle). However we could use the same method to color any shape. This concept is called
Polymorphism.
13
#Output
Parrot can f l y
Penguin ca n ' t fly
In the above program, we defined two classes parrot and penguin. Each of them have a
common fly() method. However, their functions are different.
To use polymorphism, we created a common interface i.e., flying_test() function that takes
any object and calls the object's fly() method. Thus, when we passed the blu and preggy
objects in the flying_test() function, it ran effectively.
Conclusion
Object-Oriented Programming makes the program easy to understand as well as efficient.
Since the class is sharable, the code can be reused. Data is safe and secure with data
abstraction. Polimorphism allows the same interface for different objects, so programmers
can write efficient code.
***THE END***
14