Object Oriented Programming in Python














OOP In Python:



As other general programming languages, Python is also an object-oriented programming language. By using the python object-oriented approach we can also develop application.
It allows us to create classes and objects easily. Almost everything in python is in types of objects and methods.

Major Features of Using Object-oriented Programming:

1.Class.
2.Objects.
3.Method.
4.Inheritance.
5.Polymorphism.
6.Abstraction.
7.Encapsulation.

Classes in Python:

Class is a logical grouping of related data and methods that operate on them. A class definition starts with the keyword class followed by the name of the class, and a colon. By convention, the first letter of the class name is capitalized. The syntax for class definition is as follows:

Class Class_Name:
    (Statement_1)
        -
        -
        -
    (Statement-n)

Defining a Class:

To create a class in Python, CLASS keyword is used:

class Name:
    print("Engine-4-Geeks")


Object in Python:

An object is an entity that holds some state and behaviour. It may be like any real-world entity like birds, car , bike, keyboard, chair etc. Everything in python is an object and almost everything has methods and attributes. A class declaration does not reserve memory at the time of declaration. Memory is allocated when an instance of the class is created. An instance of the class is called an Object.

Creating an Object:

name_object=myName()
print(name_object.name)


Method:

The method is a function that is associated with an object. In python, Method is not unique to class instances. Any object can have methods.

Inheritance:

Inheritance is the property by which the objects of the derived class posses the copies of the data members and member functions of the base class.
A base class is actually a superclass and derived class is a subclass.
A class that inherits or derives attributes from another class is called Derived class and the class from which it is derived is called a Base class.

Polymorphism:

The term polymorphism has been derived from the Greek words 'poly' and 'morphos' which means many and forms respectively. By polymorphism, we understand that one task can be performed in different ways.
        We have already seen that len() function operates on various types of objects such as list, str, tuples, set etc.

Encapsulation:

Encapsulation means preventing access to non-essential details i.e it is used to restrict the access of methods and variables.Code and data are both wrapped in a single entity.

Abstraction:

Data abstraction and encapsulation both are often used as synonym.Abstraction is hiding of internal details and showing only functionalities.

Post a Comment

0 Comments