OOPs in python
What is OOP's?
OOPs stands for Object-Oriented Programming Language. Object Oriented Programming (OOP) is a programming paradigm that uses classes and objects. It's utilized to break down a software program into reusable code blueprints (called classes) that may be used to build specific instances of things. In this blog I will discuss about Oops in Python specifically.
Class
A Python class is similar to a blueprint for constructing a new object. An object is something you want to do to manipulate or edit while working through the code. Every time a class object is instantiated, which is when we declare a variable, a new object is built from scratch. Class objects can be used many times if needed.
Example:
This are different type of bike, but all are called bike only, Similarly Class is collection of similar objects.
Syntax:
class <Class
Name>:
<statement>
Object
A class is instantiated as an object (instance). Only the object's description is defined when class is defined.
Syntax: <object-name> = <class-name>(<arguments>)
Inheritance
As we can use an
existing class to construct a new class rather than starting from scratch,
inheritance allows us to reuse code.
The child class
inherits the attributes of the parent class and has access to all of the data
objects and functions declared in the parent class. A child class can also
provide its own implementation for the parent class's functionalities.
A derived class can inherit a base class in Python by simply putting the base class name in brackets after the derived class name.
Syntax:
Class of BaseClass:
#Body of BaseClass
Class DerviedClass (BaseClass):
#Body of DerviedClass
Encapsulation
Encapsulation is the
process of enclosing all critical information within an object and only
revealing a selected information of it to the outside world. Inside a
class, encapsulation hides the internal software code implementation as well as
the internal data of the objects.
Methods and properties that are only accessible from other methods in the same class are known as the private/internal interface. Security is improved via encapsulation. Private attributes and methods can be defined to prevent access from outside the class. Public methods and properties are used to gather information about data in an object and to access or alter data.
Polymorphism
- Avani Popat
- Mar, 10 2022