Introduction to OOPs
OOPs का फुल फॉर्म है Object-Oriented Programming System, यह एक प्रकार है प्रोग्राम लिखने का, जिसमे Objects and Classes उपयोग किया जाता है, जो इस प्रकार से Program लिखने के तरीके का उपयोग करते है उसे Object-Oriented Programming कहते है।
Object-Oriented Programming में कुछ महत्वपूर्ण concepts है जिनको OOPs concept कहा जाता है जिनको सीख लेने के बाद हम Object-Oriented Programming में कोड लिख पाएंगे। OOPs के निम्नलिखित है –
Features of OOPs (Pillars of OOps)
- Class
- Object
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
Class
Class एक user-defined डाटा टाइप है या Class एक blue-print है किसी ऑब्जेक्ट का। Class के अंदर variable, constant एवं methods (ये function ही है, Class के अंदर है इसलिए इन्हे method कहा जाता है) आदि define रहते है जिन्हे Class के members कहा जाता है।
- Class का feature हर एक High Level Programming Language में होता है
- Class के static members को केवल Class की ही मदद एक्सेस कर किया जा सकता है
- एक Class बनने के लिए “class” keyword का उपयोग किया जाता है
- Class के abstract डाटा टाइप भी है।
Object
Object एक रियल वर्ल्ड एंटिटी है जो वास्तव में कंप्यूटर मेमोरी exists करती है, एक Class चाहे कितने भी Object बनाये जा सकते है। Object की properties हि Object के attributes कहलाते है।
उदहारण के लिए student, laptop आदि ऑब्जेक्ट है।
Explanation
जब भी हम कोई मकान, सड़क या मशीन आदि बनाते है तो बनाने से पहले एक संरचना (structure) बनाते है जिसे नक्शा या ब्लू-प्रिंट कहते है (ऐसी ब्लू-प्रिंट को Programming world में class कहते है) नक्शा बनाने के बाद मकान, सड़क या मशीन आदि कुछ रियल में जो बनाया है वह Object है।
- Class के non-static members को केवल Class का Object ही एक्सेस कर सकता है।
Example – Class & Object
#include#include class Student { private: int id; std::string name; int age; public: // Constructor Student(int i, std::string n, int a) : id(i), name(n), age(a) {} // Getter methods int getId() { return id; } std::string getName() { return name; } int getAge() { return age; } // Setter methods void setId(int i) { id = i; } void setName(std::string n) { name = n; } void setAge(int a) { age = a; } // Display method void display() { std::cout << "ID: " << id << ", Name: " << name << ", Age: " << age << std::endl; } }; int main() { Student s(1, "John Doe", 20); s.display(); s.setName("Jane Doe"); s.display(); return 0; }
public class Student { private int id; private String name; private int age; // Constructor public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } // Getter methods public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } // Setter methods public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } // Display method public void display() { System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age); } public static void main(String[] args) { Student s = new Student(1, "John Doe", 20); s.display(); s.setName("Jane Doe"); s.display(); } }
class Student: def __init__(self, id, name, age): self.id = id self.name = name self.age = age # Getter methods def get_id(self): return self.id def get_name(self): return self.name def get_age(self): return self.age # Setter methods def set_id(self, id): self.id = id def set_name(self, name): self.name = name def set_age(self, age): self.age = age # Display method def display(self): print(f'ID: {self.id}, Name: {self.name}, Age: {self.age}') if __name__ == "__main__": s = Student(1, "John Doe", 20) s.display() s.set_name("Jane Doe") s.display()
class Student { constructor(id, name, age) { this.id = id; this.name = name; this.age = age; } // Getter methods getId() { return this.id; } getName() { return this.name; } getAge() { return this.age; } // Setter methods setId(id) { this.id = id; } setName(name) { this.name = name; } setAge(age) { this.age = age; } // Display method display() { console.log(`ID: ${this.id}, Name: ${this.name}, Age: ${this.age}`); } } // Example usage const s = new Student(1, "John Doe", 20); s.display(); s.setName("Jane Doe"); s.display();
using System; public class Student { private int id; private string name; private int age; // Constructor public Student(int id, string name, int age) { this.id = id; this.name = name; this.age = age; } // Getter methods public int GetId() { return id; } public string GetName() { return name; } public int GetAge() { return age; } // Setter methods public void SetId(int id) { this.id = id; } public void SetName(string name) { this.name = name; } public void SetAge(int age) { this.age = age; } // Display method public void Display() { Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}"); } public static void Main(string[] args) { Student s = new Student(1, "John Doe", 20); s.Display(); s.SetName("Jane Doe"); s.Display(); } }
Encapsulation
डेटा (attributes) तथा फंक्शन (methods) को एक ही यूनिट में सम्मिलित करना Encapsulation कहलाता है। encapsulation को Class के मदद से achieve किया जा सकता है encapsulation की मदद से class members को direct से protect किया जाता है, ताकि क्लास डाटा एवं functions का गलत उपयोग न हो
- Encapsulation को प्राप्त करने के लिए class के members को प्राइवेट रखा जाता है एवं इनकी value set या get करने के लिए getters (value को प्राप्त के लिए) एवं setters (value को के लिए) methods का उपयोग किया जाता है।
Example
#include#include class Student { private: int id; std::string name; int age; public: // Constructor Student(int i, std::string n, int a) : id(i), name(n), age(a) {} // Getter methods int getId() { return id; } std::string getName() { return name; } int getAge() { return age; } // Setter methods void setId(int i) { id = i; } void setName(std::string n) { name = n; } void setAge(int a) { age = a; } // Display method void display() { std::cout << "ID: " << id << ", Name: " << name << ", Age: " << age << std::endl; } }; int main() { Student s(1, "John Doe", 20); s.display(); s.setName("Jane Doe"); s.display(); return 0; }
public class Student { private int id; private String name; private int age; // Constructor public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } // Getter methods public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } // Setter methods public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } // Display method public void display() { System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age); } public static void main(String[] args) { Student s = new Student(1, "John Doe", 20); s.display(); s.setName("Jane Doe"); s.display(); } }
class Student: def __init__(self, id, name, age): self.id = id self.name = name self.age = age # Getter methods def get_id(self): return self.id def get_name(self): return self.name def get_age(self): return self.age # Setter methods def set_id(self, id): self.id = id def set_name(self, name): self.name = name def set_age(self, age): self.age = age # Display method def display(self): print(f'ID: {self.id}, Name: {self.name}, Age: {self.age}') if __name__ == "__main__": s = Student(1, "John Doe", 20) s.display() s.set_name("Jane Doe") s.display()
class Student { constructor(id, name, age) { this.id = id; this.name = name; this.age = age; } // Getter methods getId() { return this.id; } getName() { return this.name; } getAge() { return this.age; } // Setter methods setId(id) { this.id = id; } setName(name) { this.name = name; } setAge(age) { this.age = age; } // Display method display() { console.log(`ID: ${this.id}, Name: ${this.name}, Age: ${this.age}`); } } // Example usage const s = new Student(1, "John Doe", 20); s.display(); s.setName("Jane Doe"); s.display();
using System; public class Student { private int id; private string name; private int age; // Constructor public Student(int id, string name, int age) { this.id = id; this.name = name; this.age = age; } // Getter methods public int GetId() { return id; } public string GetName() { return name; } public int GetAge() { return age; } // Setter methods public void SetId(int id) { this.id = id; } public void SetName(string name) { this.name = name; } public void SetAge(int age) { this.age = age; } // Display method public void Display() { Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}"); } public static void Main(string[] args) { Student s = new Student(1, "John Doe", 20); s.Display(); s.SetName("Jane Doe"); s.Display(); } }
Abstraction
Abstraction के महत्वपूर्ण OOPs concept है जिसमे Object की केवल user से अनावश्यक जानकारी छुपाना है अर्थात यूजर को केवल वही जानकारी दिखाना जो जरुरी है इसमें जो implementation की जानकारी छुपाना जाता है |
- जैसे हम कोई सॉफ्टवेयर उपयोह करते तो हमें सिर्फ features दिखाई देते है जिन्हे हमें उपयोग है लेकिन implementation की जानकारी नहीं दी जाती है की यह कैसे work कर रहा है, तो हम सकते है की implementation या internal को hide करना ही abstraction है |
Polymorphism
Polymorphism दो शब्दों से मिलकर बना हुआ है Poly (अर्थात अधिक [many] ) एवं Morphism (अर्थात फॉर्म्स) यानी many-forms (जब एक ऑब्जेक्ट, एक से अधिक फॉर्म्स में उपस्थित हो वह Polymorphism है)
Types of Polymorphism
Polymorphism निम्नलिखितदो प्रकार है-
- Compile-Time Polymorphism
- Run-Time Polymorphism
Compile-Time Polymorphism
जब एक ही नाम की दो method एक ही class में हो लेकिन signature (signature से आशय है implementation) डिफरेंट हो तब वह Compile-Time Polymorphism होता है।
- Compile-Time Polymorphism को static binding या early biding भी कहते है।
Example – Method (Function) Overloading, Operator Overloading
Run-Time Polymorphism
जब एक ही नाम की दो method हो एवं signature भी सामान हो लेकिन class डिफरेंट हो तब वह Run-Time Polymorphism होता है। (एक method parent क्लास में होगी दूसरी method चाइल्ड क्लास में होगी)
- Run-Time Polymorphism को dynamic binding या late biding भी कहते है।
Inheritance
Inheritance का अर्थ है ‘विरासत’। जब एक Class किसी दूसरे Class की प्रॉपर्टीज को Inherit (प्राप्त) करना ही, Inheritance कहलाता है।
Inheritance में, जिस क्लास (main class) से दूसरी क्लास इन्हेरिट हुई है उसे चाइल्ड क्लास या sub-class कहते है और जिस क्लास ने चाइल्ड क्लास इन्हेरिट हुई है उसे पैरेंट क्लास कहते है
Types of Inheritance
- Single Level Inheritance
- Multi-Level Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Advantages of OOPs
- Object-Oriented Programming का उपयोग करने से software की security बढ़ जाती है।
- Object-Oriented Programming के उपयोग से programs की reusability बढ़ जाती है।
- Software के मेंटेनेंस में आसानी होती है।
- Software में features addon करने में आसानी।