Python is an object-oriented programming language .You can easily create and use classes and objects in Python. OPP is the one of the most power full tool of Python. The OPP programming is to combine data and functionality and wrap it inside the object .This is called the object oriented programming . An object oriented program is based on classes and there exists a collection of interacting objects .In OPP each object can receive messages , process data and send messages to other objects .
Major principles of object oriented programming system are given below :
In an object oriented system, objects are the basic run-time entities that have state and behaviour .They may represent a person, a place or any item that the program has to handle. For example : mouse, keyboard, chair, table etc . Everything in Python is an object. Objects are the building blocks of a Python OPP program.
Class can be defined as a collection of objects . A class defines a data types ,which contains variables properties and methods. For example : if you have an employee class then it should contain an attribute and method i.e. an email id, name , age, salary etc . A class describes the abstract characteristics of a real-life things .In Python , a class is defined by using a keyword class. The keyword class is immediately followed by the name the of class. In Python, the class name must be begin from capital letter.
Syntax : class ClassName: <statement-1> . . < statement-n>
Method is a special kind of function that is defined in a class definition. In Python, method is not unique to class instances .Any object type can have methods. Methods can only be called through instances of a class or a subclass, i.e. the class name followed by a dot and the method name .Mainly there are three types of methods
1. Instance Method 2. Class Method 3. Static Method
Inheritance is a powerful feature in object oriented programming . By using the inheritance you can define a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base(or parent) class. Derived class inherits the features from the base class, adding new features to it . This results into re-usability of the code. There are five types of inheritance which are the following : 1. Single Inheritance 2. Multiple Inheritance 3. Multilevel Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
Polymorphism is made of two words “poly” and “morphs”. Poly means many and Morphs means form. It defines that one task can be performed in different ways . The polymorphism is the process of using an operator or function in different ways for different data input. In practical terms , polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can some of the things that class A does differently.
Encapsulation or Data Abstraction
The term encapsulation and abstraction (also data hiding) are often used as synonyms .Encapsulation is the mechanism for restricting the access to some of an object’s components; this means that the internal representation of an object can’t be seen from the outside of the objects definition.