Python Object Oriented Programming (With Examples)
Python Object Oriented Programming (With Examples)
An object is any entity that has attributes and behaviors. For example, a parrot is
an object. It has
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} years old")
Output
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) to our new objects.
We then accessed and assigned different values to the instance attributes using
the objects name and the . notation.
To learn more about classes and objects, visit Python Classes and Objects
(/python-programming/class)
Python
Thank you for Inheritance
printing our content at www.domain-name.com. Please check back soon for new
contents.
Inheritance is a way of creating a new class for using details of an existing class
Try hands-on Python with (https://programiz.pro/learn/master-python?
without
36% modifying
Programiz PRO!it. utm_source=sticky-
off
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
The newly(https://programiz.pro/learn/master-python?utm_source=nav-
Programiz formed class is a derived class (or child class). Similarly, the existing class
Search tutorials & examples
isPRObase floating&utm_campaign=programiz&utm_medium=referral)
a (/) class (or parent class).
www.domain-name.com
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!")
# derived class
class Dog(Animal):
def bark(self):
print("I can bark! Woof woof!!")
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
Here,
Thank youdog1 (the object
for printing of derived
our content class Dog ) can access
at www.domain-name.com. members
Please check of the
back soon forbase
new
contents.
class Animal. It's because Dog is inherited from Animal .
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
Programiz
# Calling
off members utm_source=sticky-
PRO! of the Animal class
Claim Discount Now
dog1.eat() banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-python?utm_source=nav-
dog1.sleep()
Programiz
PRO(/)
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
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 _
c = Computer()
c.sell()
Output
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.
As shown, to change the value, we have to use a setter function i.e setMaxPrice()
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 circle
def render(self):
print("Rendering Circle...")
Output
Rendering Square...
Rendering Circle...
In the above example, we have created a superclass: Polygon and two subclasses:
Square and Circle . Notice the use of the render() method.
The you
Thank main
forpurpose ofcontent
printing our the render() method is to render
at www.domain-name.com. the
Please shape.
check backHowever, the
soon for new
contents.
process of rendering a square is different from the process of rendering a circle.
Try hands-on Python with (https://programiz.pro/learn/master-python?
Hence,
36% the render()
Programiz PRO! method behaves differently in different classes. Or, we can
utm_source=sticky-
off
say Claim Discount
render() is polymorphic.
Now banner&utm_campaign=programiz&utm_medium=referral)
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/python- text=Check%20this%20amazing%20
programming/object-oriented-programming) programming/object-oriented-progra
Related Tutorials
Python Tutorial
Polymorphism in Python
Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
(/python-programming/polymorphism)
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
Python Programiz
off Tutorial PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
Python Inheritance
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(/python-programming/inheritance)
Python Tutorial
(/python-programming/class)
Python Library
Python super()
(/python-programming/methods/built-in/super)