Object Oriented Programming in Python

OOPs Concepts

* Class

* Objects

* Inheritance

* Polymorphism

* Abstraction

* Encapsulation

(toc) #title=(Types of OOP's)  

What is class?

* Python classes provide all the standard features of Object-Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with same name.

* How to create class in python
   class classname:
            class body

What are Objects?

* Class objects support two kinds of operations: attribute references and instantiation.
* Attribute references use the standard syntax used for all attribute references in Python: obj.name

What is Constructor?

* Constructor means it's a special member function which calls whenever object create of that class.
* Constructor is identified by (_) underscore.
* __init__ function: This function is called constructor in python and is call whenever the object is created of that class.
* How to create constructor in python
   def __init__(self):
              Constructor body

Program 1) #create class
class myclass:
         x=5
print(myclass)
Output = <class '__main__.myclass'>

Program 2) # Create Object
class myclass:
         a=5
b= myclass()
print(b.a)
Output = 5

Program 3) #Calling a function by object
class myclass:
           def myfunction (self):    #property of myclass
                       print('"Hello Word")
a = myclass()   #creating an object
a.myfunction()
Output= Hello Word

Program 4) #How create constructor
class myclass:
          def __init__(self):    #creating a constructor
                     print("This is my constructor")
a = myclass()    #creating an object
Output = This is my constructor 

Program 5) #change the value by object
class myclass:
         def __init__(self,name,age):
                       self.name = name
                       self.age = age
          def myfunction(self):
                     print("My name is: "+self.name)
a = myclass("Ram", 20)   #creating an object
a.name
a.age
Output = Ram,20

Program 6) #How create constructor
class myclass:
          def __init__(self):    #creating a constructor
                     print("This is my constructor")
a = myclass()    #creating an object
Output = This is my constructor 

Program 7) #change the value by object
class myclass:
         def __init__(self,name,age):
                       self.name = name
                       self.age = age
          def myfunction(self):
                     print("My name is: "+self.name)
a = myclass("Ram", 20)   #creating an object
a.age =25
a.name
print("Age: ", a1.age)
Output = Age: 25


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!