Python OOPS Concept


 Python OOPS

        Like other general-purpose programming languages, Python is also an object-oriented language. It allows us to develop applications using an Object-Oriented approach. In Python, we can easily create and use classes and objects.

An object-oriented paradigm is to design the program using classes and objects. The object is related to real-word entities such as book, house, pencil, etc. The oops concept focuses on writing the reusable code. It is a widespread technique to solve the problem by creating objects.

Major principles (features) of object oriented programming are given below : 

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

1. Object 

              The object is an real world entity that has three things. It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.Everything in Python is an object, and almost everything has attributes and methods.
  • Identity :  It gives a unique name to an object and enables one object to interact with other object.
  • State :    It is represented by attributes of an object. It also reflects the properties of an object.
  • Behaviour :  It is represented by methods of an object. It also reflects the response of an object with other objects.

Example   

Identity                           state/Attributes                            Behaviour

Name of the dog                Age                                            Bark
   (name)                          Color                                          Eat                                                                Breed                                         (methods)       
                                        (Attributes)

Dog is an object here.                           

An object is an instance of a class. such as pen,chair, table, keyboard etc. 

Everything in Python is an object and almost everything has attributes and methods. All functions have a built in attribute __doc__, which returns the docstring defined in the function source code.


 Object Creation

       When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behaviour of the class. But the values of those attributes means the state are unique for each object. A single class may have any number of instances

Syntax

object_name=class_name()

Example

class ObjectExample :

        # simple class attributes

        attr1="student"

        attr2="teacher"

        # a simple method

        def fun(self) :

                print("I am a", self.attr1)

                print("I am a", self.attr2)

# object creation (instantiation)

obj=ObjectExample()

# accessing class attributes and methods through object

print(obj.attr1)

obj.fun()

Output

student

I am a student

I am a teacher
                                                                                          


No comments:

Post a Comment