OOPs
OOPs का पूरा नाम Object-Oriented Programming system है | OPPs का मतलब होता है | कि programming को इस तरह से लिखा जाए की लगे की चीजे real world मे desgine की जा रही है | code को clean , reusable , बडी- बडी Applications बनाने के लिए OPPs का उपयोग किया जाता है | OPPs object based है | यानि की लिखा गया कोई भी code तब किसी काम का नही , जब तक उस code का object न बनाया जाए |
Class and Object
Class = Blueprint (map) Object = Actual building (इमारत) class एक Blueprint होती है | और object Actual building होती है | class के अन्दर variables , functions and method create किऐ जाते है | किसी class को बनाने के बाद वो class तब तक किसी काम की नही होती है | जब तक उस class का object न बनाया जाये | इसलिए class एक Blueprint और object एक building होती है | नीचे class और object को समझाने के लिए एक code दिया गया है |
1class example: 2 def example1(self): 3 print("hello,world") 4obj=example() 5obj.example1()
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 किसी काम की नही है |
Purpose Of OOPs
1. Code Design OOPs का सबसे अधिक उपयोग Python Programming मे code Design के
लिए किया जाता है | OOPs मे object and classes के उपयोग से चीजो को real-world के जैसे Design किया जा
सकता है |
2. Code Management बडे - बडे projects मे OOPs के उपयोग से code clean , readable बनाया
जा सकता है , यानि बडे - बडे code को readable बनाने के लिए OOPs का उपयोग किया जाता है |
3. Encapsulation Encapsulation Concept के उपयोग से projects को hide किया जा सकता है |
अपने किसी Important projects को secure बनाने के लिए OOPs का use करना |
4. complex code OPPs object based होता है तो OPPs मे हर code का object बनाया जाता है ,
इसलिए OOPs के उपयोग से code को short किया जा सकता है |
5. Inheritance एक child class
अपने parent class के code को use कर सकती है | जिसके कारण एक ही code को बार - बार लिखने की जरूरत
नही होती है |
Advantages OF OOPs
1. Inheritance Python Programming मे Inheritance Concept से एक child class
अपने parent class के code को use कर सकती है | जिसके कारण एक ही code को बार - बार लिखने की जरूरत
नही होती है | तो OOPs के use से Time की बचत होती है |
2. Encapsulation OOPs के Encapsulation Concept के उपयोग से किसी Data , Methods , Variables etc.
को private बनाया जा सकता है | जिसके कारण कोई बाहरी person data को access नही कर सकता है |
तो OOPs Data को secure बनाता है |
3. Polymorphism Concept के कारण एक ही function , method का एक से अधिक classes मे
use किया जा सकता है | Example: method overriding
4. Abstraction के उपयोग से main logics को hide किया जा सकता है | जिसके
कारण कोई बाहरी person logics को access नही कर सकता , पर logics के काम का लाभ ले सकता है |
5.Extensibility कोई new Feature add करने के लिए पूरे code को बदलने की जरूरत नही होती
code मे changes करके new Feature को add कर सकते है |
Concepts Of OOPs
Python Programming मे OOPs को maily Four Parts मे devide किया गया है | हर Concept का अपना काम है और सभी concept एक दूसरे से अलग है | इसलिए OOPs को सिखने के लिए हर concept को सिखाना जरूरी है | OOPs के सभी concepts को नीचे समझाया गया है |
1.Inheritance
Inheritance OOPs का Interesting concept है | जिसका उपयोग एक class को किसी दूसरी मे inherit करने के लिए किया जाता है
| यानि की एक class मे दूसरी class के कुछ गुण को प्राप्त करना या विरासत मे लेना | For example :- जिस प्रकार एक बेटा विरासत के रूप मे
अपने पिता से कुछ गुण प्राप्त करता है | उसी प्रकार एक child class भी अपने parent class के कुछ Methods , functions etc.
को प्राप्त करती है | Inheritance का Syntax नीचे दिया गया है |
class DerivedClass(BaseClass):
pass
Syntax Explanation
class को create करने के लिए class लिखा गया है |
DerivedClass वो class होती है जिसमे किसी दूसरी class को inherit करना है |
BaseClass वो होती है , जिसको inherit करना है |
pass की जगह code लिखा जाएगा |
Single Inheritance किसी एक class मे किसी दूसरी class को inherit करने के लिए Single Inheritance का उपयोग किया जाता है | for example :- जिस तरह एक child अपने पिता के कुछ गुण विरासत के रूप मे लेता है | किसी class को single Inheritance से inherit करने का code नीचे दिया गया है |
1class father: 2 def display(self): 3 print("I am father class") 4class Son(father): 5 def show(self): 6 print("I am Son class") 7 8object = Son() 9object.display() 10object.show()
I am Father class I am Son Class
यहाँ पर son class मे father class को inherit किया गया है | father class को inherit करने के कारण केवल Son class का object बनाया गया है | अगर son class मे father class को inherit नही किया जाता | तो Grandfather class को run करने के लिए father class का भी object create करना होता है |
Syntax Explanation
class को class से create किया जाता है |
father class का नाम है |
def display(self): display नाम के function को create करने के लिए def का use किया
गया है | self मतलब display function कोई parameter नही लेगा |
object = Son() Son class का object नाम का एक object बनाया गया है |
object.display() son class मे father class के कुछ गुण यानि function inherit हुए इसलिए
son class के object से ही father class के functions run हो जाएगे |
Multiple Inheritance किसी एक class मे एक से अधिक class को inherit करने के लिए Multiple Inheritance का उपयोग किया जाता है |
For Example
father और mother नाम की 2 classes है और son class मे father और mother class को inherit किया गया है | इस तरह से son class मे
father और mother class के गुण inherit हुए है | Multiple Inheritance से classes को inherit करने का code नीचे दिया गया है |
1class Father: 2 def display_Father(self): 3 print("I am Father class") 4class Mother: 5 def display_Mother(self): 5 print("I am Son class") 5class Son (Father , Mother): 5 def display_Son(self): 5 print("I am Son Class") 5 5object = Son() 5object.display_Father() 5object.display_Mother() 5object.display_Son()
I am Gradfather class I am Son class
Syntax Explanation
class Son (Father , Mother): son एक drived class है | जिसमे Father और Mother class को
inherit किया गया है |
object = Son() Son class का object नाम का एक object create किया गया है | Because
son class मे father और mother class के functions inherit हुए है , इसलिए son class के create किए गए object से
ही father और mohter class run हो जाएगी |
Multilevel Inheritance
पहली class को दूसरी class मे दूसरी class को तीसरी class मे inherit करना Multilevel Inheritance होता है |
For Example
image के use से नीचे Multilevel Inheritance को perfectly समझाया गया है | Grandfather , Father और Son नाम की तीन classes है | जिस तरह से image मे Grandfather class के कुछ गुण Father class मे आते है | Father class के कुछ गुण Son class मे आते है | उसी तरह से एक class को दूसरी class मे दूसरी class को तीसरी class .. inherit करने के लिए Multilevel Inheritance का उपयोग किया जाता है | Multilevel Inheritance का नीचे code दिया गया है |
1class Grandfather: 2 def display_Grandfather(self): 3 print("I am Grandfather class") 4class Father(Grandfather): 5 def display_Father(self): 6 print("I am Father class") 7class Son(Father): 8 def display_Son(self): 9 print("I am Son Class") 10 5object = Son() 5object.display_Grandfather() 5object.display_Father() 5object.display_Son()
I am Grandfather class I am Father class I am Son Class
Hybrid-Inheritance
किसी एक class को एक से अधिक classes मे inherit करने के लिए Hybrid Inheritance का उपयोग किया जाता है | जैसे उपर दिए गए image मे father class को son1 , son2 और son3 classes मे inherit किया गया है | Hybrid Inheritance का code नीचे दिया गया है |
code Explanation
1class Father: 2 def show1(self): 3 print("I am Father class") 4class child1(Father): 5 def show2(self): 6 print("I am Child1 class") 7class child2(Father): 8 def show3(self): 9 print("I am Child2 Class") 10 11object1 = child1() 12object2 = child2() 13 14object1.show2() 15object1.show1() 15object2.show3() 15object2.show1()
I am Child1 class I am Father class I am Child2 Class I am Father class
code Explanation
Father , child1 , child2 नाम की तीन classes create की गई है | Father class को child1 और child2 class मे inherit किया गया है | child1 class और child2 class का object create किया गया है |
Hybrid Inheritance5.Hierarchical Inheritance
जब एक ही माता-पिता (Parent Class) से कई बच्चे (Child Classes) बनते हैं , उसे Hierarchical Inheritance कहते हैं |
जैसे image मे दिखाया गया है कि एक father से 2 son और उन sons के childs .
Multilevel Inheritance का नीचे code दिया गया है |
1class A: 2 def show1(self): 3 print("I am A class") 4class B(A): 5 def show2(self): 6 print("I am B class") 7class C(A): 8 def show3(self): 9 print("I am C Class") 10class D(B , C): 11 def show4(self): 12 print("I am D Class") 13 14 15object = D() 16object.show1() 17object.show2() 18object.show3() 19object.show4()
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 ना कर सके |
Rules :
Encapsulation का उपयोग करने के rules नीचे दिए गए है |
1. Variable को hide करना Encapsulation का उपयोग ही data को secure बनाने के लिए किया
जाता है और data को variables मे store किया जाता है , इसलिए variable को ही hide कर देने से data secure हो जाएगा
variables को private बनाने के लिए __ (double underscore) का use किया जाता है | किसी भी variable के आगे
__ (double underscore) का उपयोग करने से वो variable private बन जाता है |
2. Getter and setter method अगर variables को ही hide कर दिया जाएगा तो data तो access नही किया
जाएगा | लेकिन लिखे गए program का use भी नही हो पाएगा , इसलिए variables को private बनाने के बाद Getter
and setter method का use करे ताकि data के साथ काम किया जा सके |
3. same place Encapsulation का नियम है कि create किए गए data (variables) और उन पर काम करने वाले
methods को एक ही जगह रखा जाए |
4. Data Hiding User को केवल methods का interface पता होना चाहिए,
अंदर data कैसे store/modify हो रहा है — यह hidden रहना चाहिए | नीचे एक Encapsulation का उदाहरण दिया गया है |
1class Fruits: 2 def set(self, name): 3 def set(self, name): 4 5 def show_name(self): 6 print("The fruit name is", self.__name) 7 8object = Fruits() 9object.set("Apple") 10object.show_name()
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 OOPs का एक बहुत ही Interesting और महात्वपूर्ण concept है | Polymorphism का मतलब है कि
"एक नाम , कई रूप" | यानि Polymorphism Python मे किसी एक ही नाम के function , method ,class को अलग -
अलग काम करने की आज़ादी देता है |
“Poly” का मतलब है कई, और “Morph” का मतलब है रूप।
इसलिए Polymorphism = कई रूपों में काम करने की क्षमता।
For Example तीन classes बनाई गई तीनो classes मे एक ही function का होना |
run करने पर तीनो classes मे से कब कौन-सा function run होगा | इसके लिए
Polymorphism का उपयोग किया जाएगा |
नीचे Polymorphism का code दिया गया है | जिसमे
2 classes create की गई है और दोनों classes मे sound नाम का function create किया गया है | अब दोनों function मे से कब कौन - सा function चलेगा | ये तय करने के लिए Polymorphism का उपयोग किया गया है |
1class Cat: 2 def sound(self): 3 return "Meow" 4class Dog: 5 def sound(self): 6 return "Bark" 7 8def listen_sound(animal): 9 print(animal.sound()) 10 11dog = Dog() 12cat = Cat() 13 14listen_sound(dog) 15listen_sound(cat)
Meow Bark
code Explanation
class Cat: cat नाम की class create की गई है |
def sound(self): sound नाम का function create किया गया है | जो कोई parameter नही लेता है |
return "Meow" cat class के sound function को call करने पर sound function Meow को return करेगा |
class Dog: इसी तरह Dog class को create किया गया है |
def listen_sound(animal): classes के बाहर listen_sound नाम का function create किया गया है |
जिसमे animal नाम के parameter को pass किया गया है |
print(animal.sound()) animal sound function को print करेगा |
dog = Dog() Dog class का dog नाम का object create किया गया है |
cat = Cat() Cat class का cat नाम का object create किया गया है |
listen_sound(dog) listen_sound function को call की गई है | जिसमे dog object को pass किया गया है |
listen_sound(cat)listen_sound function को call की गई है | जि स मे cat object को pass किया गया है
Polymorphism के Rules
1. classes मे same functions , methods name होने चाहिए |
2. classes मे same functions , methods name same होने चाहिए | पर हर class के according हर function का अपना अलग
काम होना चाहिए |
3. Polymorphism मे किसी दूसरी class को inherit करने जरूत नही है |
4. Operators जैसे +, * अलग-अलग data type पर अलग तरह से काम करते हैं | यह भी Polymorphism का एक rule है |
5. Polymorphism मे Functions , Methods , variables public हो ने चाहिए Private Functions etc. होने पर Polymorphism
concept काम न ही करता है |
Polymorphism के लाभ
1. create किए गए एक ही function , method से code को control किया जा सकता है |
2. defin किए गए पूरे code मे changes करने की बजाए केवल function मे changes कर सकते है |
3. same एक ही class को भी create किया जा सकता है | जिससे पूरे बडे code को लिखने की जरूत नही |
4. Polymorphism flexible architecture तैयार करने में मदद करता है।
5. एक जैसा function नाम रखने से program easy और साफ़ रहता है।
4.Constructor
Python Programming मे Constructor एक method है | जो अपने आप class का object बनाने पर run हो जाता है | Constructor को __init__() से defin किया जाता है | Constructor को create करने का Syntax नीचे दिया गया है |
1class ClassName: 2 def __init__(self, parameters):
Syntax Explanation
class ClassName: class से class create की जाती है | ClassName class का नाम देने के लिए |
def __init__(self, parameters): def से function create किया जाता है | __init__
से Constructor create किया जाता है |
नीचे एक Constructor को create करने का code दिया गया है |
1class name: 2 def __init__ (self): 3 print("my name is Shubham") 4 5object = name()
my name is Shubham
code Explanation
class name: name नाम की class create की गई है |
def __init__ (self): Constructor create किया गया है |
object = name() name नाम की class का object create किया गया है |
key points
1. constructor को __init__ से create किया जाता है |
2. class का object बनाते class मे create किए गए functions , methods अपने आप call हो जाते है |
3. Self constructor के अंदर object को represent करता है
4. Parameterized और default दो types commonly होते हैं
Constructor के लाभ
1. run time पर class अपने आप run हो जाता है |
2. केवल class का object बनाने की जरूत object से class के function , methods को call करने की जरूत नही |
3. Constructor के use से functions मे values set करने का अपना standard है |
4. Constructor के use से लिखा गया code clean और समझने आसान हो जाता है |
अध्याय समाप्त — आगे बढ़ें
आपने इस chapter मे python के बहुत ही महत्वपूर्ण concepts समझे है जो आगे की प्रोग्रामिंग सीखने मे बहुत Helpful हैं। आगे के chapter मे आप Mysql के बारे मे सिखेगे आगे के chapter की और बढ़ने के लिए नीचे दिए गए बटन पर क्लिक करे |
Next Chapter
Seekho Coding