Python Full Course In Hindi

Home About Us Contact Us Privacy Policy

OOPs

OOPs का पूरा नाम Object-Oriented Programming system है | OPPs का मतलब होता है | कि programming को इस तरह से लिखा जाए की लगे की चीजे real world मे desgine की जा रही है | code को clean , reusable , बडी- बडी Applications बनाने के लिए OPPs का उपयोग किया जाता है | OOPs के main चार Concepts है |
1.Inheritance
2.Encapsulation
3.Polymorphism
4.Constructor

Class and Object


Class = Blueprint (खाका)
Object = Actual building (इमारत)
class एक Blueprint होती है | और object Actual building होती है |

class के अन्दर varrible function and method create किऐ जाते है | किसी class को बनाने के बाद वो class तब तक किसी काम की नही होती है | जब तक उस class का object न बनाया जाये | इसलिए class एक Blueprint और object एक building होती है |

🐍 Syntax
class example: def example1(self): print("hello,world") obj=example() obj.example1()
Output
hello,world

Syntax Explanation

class class को create करने के लिए
example class का नाम है |
def function create करने के लिए
example1 function का नाम
obj=example() example नाम की class का object बनाया गया है |
obj.example1() example नाम की class के example1 नाम के function को call करने के लि ए obj.example1 लिखा गया है |
जब तक class का object न ही बनाया जाता तब तक वो class किसी काम की नही है |

1.Inheritance

Inheritance का मतलब है कि एक class को किसी दूसरी मे inherit करना | यानि की एक class मे दूसरी class के कुछ गुण को प्राप्त करना या विरासत मे लेना |
For Example
जिस प्रकार childs कुछ गुण अपने parents से प्राप्त करते है |
Inheritance 5 प्रकार की होती है |
1.Single Inheritance
2.Multiple Inheritance
3.Multilevel Inheritance
4.Hybrid Inheritance
5.Hierarchical Inheritance
Single Inheritance Single Inheritance का मतलब है कि एक class को किसी दूसरी मे inherit करना |

🐍 Syntax
class Gradfather: def display(self): print("I am Gradfather class") class Son(Gradfather): def show(self): print("I am Son class") object = Son() object.display() object.show()
Output
I am Gradfather class
I am Son class

यहाँ पर son class मे Gradfather class को inherit किया गया है | Gradfather class को inherit करने के कारण केवल Son class का object बनाया गया है | अगर son class मे Gradfather class को inherit नही किया जाता | तो Grandfather class को run करने के लिए Gradfather class का भी object create करना होता है |

Multiple Inheritance का मतलब है कि एक class मे एक से ज़्यादा classes को inherit करना |
For Example
माता और पिता की कुछ qualities का child प्राप्त होना |

🐍 Syntax
class Father: def display_Father(self): print("I am Father class") class Mother: def display_Mother(self): print("I am Mother class") class Son (Father , Mother): def display_Son(self): print("I am Son Class") object = Son() object.display_Father() object.display_Mother() object.display_Son()
Output
I am Gradfather class
I am Son class

Multilevel Inheritance मे पहली class दूसरी class से दूसरी class तीसरी class से inherit की जाती है |
For Example
Grandfather की कुछ qualities का Father मे आना Father की कुछ qualities का Son मे आना |

🐍 Syntax
class Grandfather: def display_Grandfather(self): print("I am Grandfather class") class Father(Grandfather): def display_Father(self): print("I am Father class") class Son(Father): def display_Son(self): print("I am Son Class") object = Son() object.display_Father() object.display_Mother() object.display_Son()
Output
I am Grandfather class
I am Father class
I am Son Class

Hybrid Inheritance मे एक class को कई class मे inherit किया जाता है |
For Example
एक पिता एक से अधिक childs

🐍 Syntax
class Father: def show1(self): print("I am Father class") class child1(Father): def show2(self): print("I am Child1 class") class child2(Father): def show3(self): print("I am Child2 Class") object1 = child1() object2 = child2() object1.show1() object1.show2() object2.show1() object2.show3()
Output
I am Father class
I am Child1 class
I am Father class
I am Child2 Class

5.Hierarchical Inheritance जब उपर के 2 या 2 से अधिक Uses किए जाए |

🐍 Syntax
class A: def show1(self): print("I am A class") class B(A): def show2(self): print("I am B class") class C(A): def show3(self): print("I am C Class") class D(B , C): def show4(self): print("I am D Class") object = D() object.show1() object.show2() object.show3() object.show4()
Output
I am A class
I am B class
I am C class
I am D class

2. Encapsulation

अपने data को सुरक्षित रखने के लिए Encapsulation का उपयोग किया जाता है | Encapsulation का मतलब है | कि data को छुपाना ताकि कोई भी बाहरी इंसान data को बिना permission के access ना कर सके | __(Single underscore) या ___ (double underscore) से varribles को private बनाते है | या hide करते है |

🐍 Syntax
class Fruits: def set(self): self.__name = "Apple" def show_name(self): print("The fruit name is", self.__name) object = Fruits() object.show_name()
Output
The fruit name is Apple

Syntax Explanation

1. class Fruits = Fruits नाम की class create करने के लिए
2. def = function create करने के लिए
3. set = function का नाम
4. (self) = कोई arrgument pass नही किया गया है |
5. self.__name = name varrible को private varrible बनाने के लिए
6. "Apple" = name varrible मे value store की गई है |
7. object = Fruits() = Fruits नाम की class का object बनाने के लिए
8. object.show_name() = show_name function को run करने के लिए
9. name = एक varrible है |

Polymorphism

Polymorphism का मतलब है कि एक ही चीज के कई रूप होना |
For Example
तीन classes बनाई गई तीनो classes मे एक ही function का होना |
run करने पर तीनो classes मे से कब कौन-सा function run होगा इसके लिए Polymorphism का उपयोग किया जाता है |

🐍 Syntax
class Cat: def sound(self): return "Meow" class Dog: def sound(self): return "Bark" def listen_sound(animal): print(animal.sound()) dog = Dog() cat = Cat() dlisten_sound(dog) listen_sound(cat)
Output
Meow
Bark

4.Constructor

Python Programming मे Constructor एक method है | जो अपने आप class का object बनाने पर run हो जाता है | Constructor को __init__() से से defin किया जाता है |

Example
🐍 Syntax
class name(): def __init__ (self): print("my name is Shubham") object = name()
Output
my name is Shubham

यहाँ पर केवल name नाम की class का object बनाया गया है |