OOP Concepts in Python with Simple Examples
OOP Concepts in Python with Simple Examples
Areas Covers
- Class and Objects
- Inheritance
- Encapsulation
- Polymorphism
- Abstraction
Class and Objects
- Python is a object oriented programming language
- So Everything in Python is based on Objects
- Object is simply a collection of data(variables) and methods(functions) which act on this data.
- Class is a blueprint for the object
As we use "def" when defining a function, we use class when defining a class
| Class and Object of the class |
class Person
- Here Person class is defined with two variables(firstName, lastName) and a method(basicDetails()).
- __init__ is a constructor of the class. And it is called when initializing the objects.
- Normally we declare all the variables in __init__ function only.
- Note: The self parameter is a reference to the current instance of the class, and is used to access variables which belongs to the class.
- So the variables declared in constructor can be accessed using self only in above example.
- We can use any word as a 1st parameter of the constructor not only self.
- Once defining the methods in class we should use self as parameter.
Initializing Object
- obj is an object of the class Person
- Using that object we can access the variables and methods in the class as shown in examples
Inheritance
Inheritance is accessing parent class properties(Variables and Methods) from child class.
- We have defined two child classes(Student and Teacher) for Parent(Person) class.
- The objects are created to the child classes only and accessed the properties of parent class.
- Note: a class should contain some properties when defining it. If we have no any property, we should use the keyword pass in the body.
- If we use __init__ function in child class, then we can not access parent __init__ function anymore except calling the parent __init__ inside the child __init__ function using super() or parent class name as shown in the example.
- Parent class also called Base class
- Child class also called sub class
Encapsulation
It describes the hiding data and methods from other classes.
It puts restrictions on accessing data directly prevent the accidental modification of data.
It is used as an object variable can be changed by an object's method only.
| Encapsulation |
Access Modifiers
- Public can be accessed from inside the class and outside the class
- Protected can be accessed from inside the class and outside the class in the same package
- Private can be accessed only inside the class
- "_" is used to define the protected variables
- "__" is used to define the private variables
In above example,
_firstName is a protected variable
__lastName is a private variable
If we try accessing private variable outside the class by uncommenting, it will give an attribute error.
But we can access it through the method of that class as above example.
Polymorphism
Polymorphism simply means having many forms
| Polymorphism |
When we define a method in child class same as parent class and create an object for child class, the method which is in child class only will executed not which is in parent class. That is called method overriding.
Abstraction
We can use Abstraction for hiding the internal details or implementation of a function and showing its functionalities only.
Any class with one abstract function is an Abstract class.
In order to create an Abstract class, first we have to import ABC class from abc module as follows,
"from abc import ABC"
ABC stands for "Abstract Based Class"
Note: We can not create an object for Abstract class directly. We can create an object only to the child class of an abstract class.
| Abstraction |
Person is an Abstract class which we can not create an object for it.
We can create an object to child class Student.
Hope this blog gave you in understanding OOP Concepts of Python. You can contact me through
Thank You
Comments
Post a Comment