E2UC202C Unit 1 Lec 2- Object Oriented Paradigm
E2UC202C Unit 1 Lec 2- Object Oriented Paradigm
UNIT I
Introduction: Basic Terminology
An object is any entity that has attributes and behaviors. For example, a parrot is an
object. It has
class Parrot:
# class attribute
name = ""
age = 0
# create parrot1 object
parrot1 = Parrot()
parrot1.name = "Blu"
parrot1.age = 10
Python Class and Object
Output
Blu is 10 years old
Woo is 15 years old
In the above example, we created a class with the name Parrot with two attributes: name and age.
Then, we create instances of the Parrot class. Here, parrot1 and parrot2 are references (value) our
new objects.
We then accessed and assigned different values to the instance attributes using the objects name
and the . notation.
Python 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).
Example 2: Use of Inheritance in Python
# base class
class Animal:
def eat(self):
print( "I can eat!")
def sleep(self):
print("I can sleep!")
Python Inheritance
# derived class
class Dog(Animal):
def bark(self):
print("I can bark! Woof woof!!")
I can eat!
I can sleep!
I can bark! Woof woof!!
Here, dog1 (the object of derived class Dog) can access members of
the base class Animal. It's because Dog is inherited from Animal.
# Calling members of the Animal class
dog1.eat()
dog1.sleep()
Python Encapsulation
Encapsulation is one of the key features of object-oriented programming.
Encapsulation refers to the bundling of attributes and methods inside a single
class.
It prevents outer classes from accessing and changing attributes and methods of a
class. This also helps to achieve data hiding.
In Python, we denote private attributes using underscore as the prefix i.e single _
or double __. For example,
class Computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = Computer()
c.sell()
# change the price
c.__maxprice = 1000
c.sell()
# using setter function
c.setMaxPrice(1000)
c.sell()
Output
Selling Price: 900
Selling Price: 900
Selling Price: 1000
In the above program, we defined a Computer class.
We used __init__() method to store the maximum selling price of
Computer. Here, notice the code
c.__maxprice = 1000
Here, we have tried to modify the value of __maxprice outside of the
class. However, since __maxprice is a private variable, this
modification is not seen on the output.
Polymorphism
Polymorphism is another important concept of object-oriented programming. It
simply means more than one form.
That is, the same entity (method or operator or object) can perform different
operations in different scenarios.
Let's see an example,
class Polygon:
# method to render a shape
def render(self):
print("Rendering Polygon...")
class Square(Polygon):
# renders Square
def render(self):
print("Rendering Square...")
class Circle(Polygon):
# renders Square
def render(self):
print("Rendering Circle...")
# create an object of Square
s1 = Square()
s1.render()
# create an object of Circle
In the above example, we have created a superclass: Polygon and two
c1 = Circle() subclasses: Square and Circle. Notice the use of the render() method.
c1.render() The main purpose of the render() method is to render the shape. However,
the process of rendering a square is different from the process of rendering
a circle.
Output
Hence, the render() method behaves differently in different classes. Or, we
Rendering Square... can say render() is polymorphic.
Rendering Circle...
Contact Information