Object-Oriented Programming (OOP) Principles – ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग के सिद्धांत
Object-Oriented Programming (OOP) is a programming approach that organizes a program around objects and classes rather than only functions and procedures. It helps developers represent real-world entities in programs and makes software easier to organize, reuse, extend, and maintain. Java is primarily an object-oriented programming language. Its OOP approach is based on concepts such as objects, classes, abstraction, encapsulation, inheritance, and polymorphism. Dynamic binding and message passing are also important concepts in object-oriented programming.
ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) एक ऐसी प्रोग्रामिंग पद्धति है जिसमें प्रोग्राम को केवल फंक्शन और प्रक्रियाओं के बजाय ऑब्जेक्ट और क्लास के आधार पर व्यवस्थित किया जाता है। यह वास्तविक दुनिया की वस्तुओं को प्रोग्राम में दर्शाने तथा सॉफ्टवेयर को व्यवस्थित, पुनः उपयोग योग्य, विस्तार योग्य और आसानी से मेंटेन करने योग्य बनाती है। Java मुख्य रूप से एक ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग भाषा है। इसमें ऑब्जेक्ट, क्लास, एब्स्ट्रैक्शन, एनकैप्सुलेशन, इनहेरिटेंस और पॉलीमॉर्फिज्म जैसी अवधारणाओं का उपयोग किया जाता है। डायनामिक बाइंडिंग और मैसेज पासिंग भी ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग की महत्वपूर्ण अवधारणाएँ हैं।
1. Object (ऑब्जेक्ट)
An object is an instance of a class. It is a basic runtime unit of object-oriented programming. An object has three main characteristics:
State: Data or attributes represented by variables.
Behavior: Actions represented by methods.
Identity: A unique identity that distinguishes it from other objects.
Examples of real-world objects: Car, Student, Account, Mobile Phone.
Java Example
class Student {int rollNo;String name;void display() {System.out.println(rollNo + " " + name);}}public class Main {public static void main(String[] args) {Student s = new Student();s.rollNo = 101;s.name = "Amit";s.display();}}
Output:
101 AmitIn this example, s refers to an object created from the Student class. The object has data (rollNo, name) and behavior (display()).
हिन्दी में समझें: ऑब्जेक्ट किसी क्लास का इंस्टेंस होता है। इसमें तीन मुख्य विशेषताएँ होती हैं—स्टेट (डेटा), बिहेवियर (कार्य) और आइडेंटिटी (पहचान)। उदाहरण के लिए, Student क्लास से बनाया गया प्रत्येक विद्यार्थी का ऑब्जेक्ट अपने नाम और रोल नंबर जैसी जानकारी रख सकता है।
2. Class (क्लास)
A class is a blueprint or template used to create objects. It defines the data members (variables) and methods that its objects can have. A class is also commonly described as a user-defined reference type in Java.
Java Example
class Employee {int empId;String empName;void display() {System.out.println(empId + " " + empName);}}
Creating an object:
Employee e = new Employee();Here, Employee is a class, while e is a reference variable that refers to the newly created object.
हिन्दी में समझें: क्लास एक ब्लूप्रिंट या टेम्पलेट होती है, जिसके आधार पर ऑब्जेक्ट बनाए जाते हैं। इसमें डेटा मेंबर्स और मेथड्स परिभाषित किए जाते हैं। Java में क्लास एक यूजर-डिफाइंड रेफरेंस टाइप होती है।
3. Data Abstraction (डेटा एब्स्ट्रैक्शन)
Data Abstraction means hiding unnecessary implementation details and showing only the essential features of an object. It helps users focus on what an object does rather than how it works internally.
In Java, abstraction can be achieved using:
Abstract classes
Interfaces
Java Example
abstract class Vehicle {abstract void start();}class Car extends Vehicle {void start() {System.out.println("Car starts with a key");}}public class Main {public static void main(String[] args) {Vehicle v = new Car();v.start();}}
Output:
Car starts with a keyThe user interacts with the start() method without needing to know all the internal details of how the car starts.
हिन्दी में समझें: डेटा एब्स्ट्रैक्शन का अर्थ अनावश्यक आंतरिक विवरणों को छिपाकर केवल आवश्यक जानकारी या सुविधाएँ दिखाना है। Java में Abstract Class और Interface की सहायता से एब्स्ट्रैक्शन प्राप्त किया जाता है।
4. Encapsulation (एनकैप्सुलेशन)
Encapsulation is the process of combining data and the methods that operate on that data into a single unit called a class. It also helps protect data by controlling access through access modifiers such as private, protected, and public.
Java Example
class BankAccount {private double balance;public void deposit(double amount) {if (amount > 0) {balance += amount;}}public double getBalance() {return balance;}}public class Main {public static void main(String[] args) {BankAccount account = new BankAccount();account.deposit(5000);System.out.println(account.getBalance());}}
Output:
5000.0Here, the balance variable is private and cannot be accessed directly from outside the class. It is accessed through public methods.
हिन्दी में समझें: एनकैप्सुलेशन वह प्रक्रिया है जिसमें डेटा और उस पर कार्य करने वाले मेथड्स को एक ही क्लास में संयोजित किया जाता है। private जैसे एक्सेस मॉडिफायर का उपयोग करके डेटा तक पहुँच को नियंत्रित किया जा सकता है। इससे डेटा की सुरक्षा और नियंत्रण बेहतर होता है।
5. Inheritance (इनहेरिटेंस)
Inheritance is an OOP feature in which one class acquires accessible properties and methods of another class. The existing class is called the superclass or parent class, while the new class is called the subclass or child class.
Java supports the following common forms of inheritance through classes:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Java does not support multiple inheritance through classes directly. However, a class can implement multiple interfaces.
Java Example
class Animal {void eat() {System.out.println("Animal eats food");}}class Dog extends Animal {void bark() {System.out.println("Dog barks");}}public class Main {public static void main(String[] args) {Dog d = new Dog();d.eat();d.bark();}}
Output:
Animal eats foodDog barks
The Dog class inherits the eat() method from the Animal class using the extends keyword.
हिन्दी में समझें: इनहेरिटेंस वह प्रक्रिया है जिसमें एक क्लास दूसरी क्लास के उपलब्ध डेटा और मेथड्स को प्राप्त करती है। पुरानी क्लास को Parent Class या Superclass तथा नई क्लास को Child Class या Subclass कहते हैं। इससे कोड का पुनः उपयोग करना आसान होता है।
6. Polymorphism (पॉलीमॉर्फिज्म)
The word Polymorphism means "many forms." It allows the same method name or reference to represent different behaviors in different situations.
Java supports two major forms of polymorphism:
A. Compile-Time Polymorphism (Method Overloading)
Method overloading occurs when multiple methods in the same class have the same name but different parameter lists.
class Calculator {int add(int a, int b) {return a + b;}double add(double a, double b) {return a + b;}}public class Main {public static void main(String[] args) {Calculator c = new Calculator();System.out.println(c.add(10, 20));System.out.println(c.add(5.5, 2.5));}}
Output:
308.0
B. Runtime Polymorphism (Method Overriding)
Runtime polymorphism occurs when a subclass provides its own implementation of an inherited method. The method implementation to execute is determined at runtime for an overridden instance method.
class Animal {void sound() {System.out.println("Animal makes a sound");}}class Dog extends Animal {@Overridevoid sound() {System.out.println("Dog barks");}}public class Main {public static void main(String[] args) {Animal a = new Dog();a.sound();}}
Output:
Dog barksहिन्दी में समझें: पॉलीमॉर्फिज्म का अर्थ है—एक नाम, अनेक रूप। Java में Method Overloading द्वारा Compile-Time Polymorphism और Method Overriding द्वारा Runtime Polymorphism प्राप्त किया जाता है। इससे एक ही मेथड नाम अलग-अलग परिस्थितियों में अलग व्यवहार कर सकता है।
7. Dynamic Binding (डायनामिक बाइंडिंग)
Binding means associating a method call with the actual method implementation. Dynamic Binding, also known as Late Binding, occurs when the method implementation is selected at runtime.
In Java, overridden instance methods use dynamic method dispatch.
Java Example
class Animal {void sound() {System.out.println("Animal makes a sound");}}class Cat extends Animal {@Overridevoid sound() {System.out.println("Cat meows");}}public class Main {public static void main(String[] args) {Animal a = new Cat();a.sound();}}
Output:
Cat meowsAlthough the reference variable is of type Animal, the actual object is of type Cat. Therefore, the overridden sound() method of the Cat class is executed at runtime.
हिन्दी में समझें: डायनामिक बाइंडिंग वह प्रक्रिया है जिसमें किसी मेथड कॉल को उसके वास्तविक मेथड इम्प्लीमेंटेशन से रनटाइम पर जोड़ा जाता है। Java में ओवरराइड किए गए Instance Methods के लिए यह प्रक्रिया उपयोग होती है। यह Runtime Polymorphism का आधार है।
8. Message Passing (मैसेज पासिंग)
Message Passing is the process through which objects communicate with each other by invoking methods. One object can request another object to perform an operation by calling its method.
Java Example
class Student {void display() {System.out.println("Student details displayed");}}public class Main {public static void main(String[] args) {Student s = new Student();s.display();}}
Output:
Student details displayedHere, the main() method invokes the display() method through the object reference s. This method invocation represents message passing.
हिन्दी में समझें: मैसेज पासिंग वह प्रक्रिया है जिसके माध्यम से ऑब्जेक्ट एक-दूसरे से संवाद करते हैं। एक ऑब्जेक्ट दूसरे ऑब्जेक्ट के मेथड को कॉल करके उससे कोई कार्य करने का अनुरोध कर सकता है। Java में मेथड कॉल इस प्रकार के संवाद का उदाहरण है।
Summary Table: Principles of OOP (OOP के सिद्धांतों का सारांश)
| Principle | Description (English) | विवरण (हिन्दी) |
|---|---|---|
| Object | Instance of a class with state, behavior and identity | क्लास का इंस्टेंस जिसमें स्टेट, बिहेवियर और पहचान होती है |
| Class | Blueprint for creating objects | ऑब्जेक्ट बनाने का ब्लूप्रिंट |
| Data Abstraction | Hiding implementation details | आंतरिक कार्यप्रणाली के विवरण छिपाना |
| Encapsulation | Combining data and methods in a class | डेटा और मेथड्स को एक क्लास में संयोजित करना |
| Inheritance | Acquiring features of another class | दूसरी क्लास के उपलब्ध गुण और मेथड प्राप्त करना |
| Polymorphism | One name with different behaviors | एक नाम के अनेक रूप |
| Dynamic Binding | Selecting overridden method implementation at runtime | ओवरराइड मेथड का चयन रनटाइम पर होना |
| Message Passing | Communication through method calls | मेथड कॉल द्वारा ऑब्जेक्ट्स का संवाद |
Object-Oriented Programming provides a structured way to develop software using objects and classes. Concepts such as abstraction, encapsulation, inheritance, and polymorphism help make programs reusable, flexible, and maintainable. These concepts form an important foundation for learning Java and developing real-world applications.
ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग ऑब्जेक्ट और क्लास की सहायता से सॉफ्टवेयर विकसित करने का एक व्यवस्थित तरीका प्रदान करती है। एब्स्ट्रैक्शन, एनकैप्सुलेशन, इनहेरिटेंस और पॉलीमॉर्फिज्म जैसी अवधारणाएँ प्रोग्राम को पुनः उपयोग योग्य, लचीला और आसानी से मेंटेन करने योग्य बनाती हैं। Java सीखने और वास्तविक एप्लिकेशन विकसित करने के लिए ये अवधारणाएँ महत्वपूर्ण आधार प्रदान करती हैं।
Comments
Post a Comment