Java Constructor जावा में कंस्ट्रक्टर, Default Constructor डिफ़ॉल्ट कंस्ट्रक्टर, Parameterized Constructor पैरामीटराइज्ड कंस्ट्रक्टर, Constructor Overloading कंस्ट्रक्टर ओवरलोडिंग, Difference between Constructor and Method कंस्ट्रक्टर और मेथड में अंतर, Copy Constructor Alternative in Java कॉपी कंस्ट्रक्टर का विकल्प

🏗️ Java Constructor (कंस्ट्रक्टर)

A constructor in Java is a special method that is called automatically when an object of a class is created. It initializes the object with default or custom values.

जावा में कंस्ट्रक्टर एक विशेष मेथड होता है, जो किसी क्लास के ऑब्जेक्ट के बनते ही स्वतः कॉल हो जाता है। यह ऑब्जेक्ट को प्रारंभिक या कस्टम मान प्रदान करता है।


📚 Types of Constructors in Java (जावा में कंस्ट्रक्टर के प्रकार)

There are three main types of constructors in Java:

  • Default Constructor

  • Parameterized Constructor

  • Constructor Overloading

जावा में तीन मुख्य प्रकार के कंस्ट्रक्टर होते हैं:

  • डिफ़ॉल्ट कंस्ट्रक्टर

  • पैरामीटराइज्ड कंस्ट्रक्टर

  • कंस्ट्रक्टर ओवरलोडिंग


✅ Default Constructor (डिफ़ॉल्ट कंस्ट्रक्टर)

A default constructor does not take any arguments and assigns the same default value to all objects.

डिफ़ॉल्ट कंस्ट्रक्टर कोई पैरामीटर नहीं लेता और सभी ऑब्जेक्ट को समान प्रारंभिक मान देता है।

class Test {
int a, b;
Test() {
a = 25;
b = 36;
System.out.println("Value of a: " + a);
System.out.println("Value of b: " + b);
}
public static void main(String[] args) {
Test t = new Test();
}
}

🔢 Parameterized Constructor (पैरामीटराइज्ड कंस्ट्रक्टर)

This type of constructor accepts parameters to assign different values to different objects.

यह कंस्ट्रक्टर पैरामीटर स्वीकार करता है ताकि विभिन्न ऑब्जेक्ट को अलग-अलग मान दिए जा सकें।

class Test {
int a, b;
Test(int x, int y) {
a = x;
b = y;
}
void display() {
System.out.println("Addition: " + (a + b));
}
public static void main(String[] args) {
Test t = new Test(25, 36);
t.display();
}
}

♻️ Constructor Overloading (कंस्ट्रक्टर ओवरलोडिंग)

Constructor overloading means having more than one constructor in a class with different parameter lists.

कंस्ट्रक्टर ओवरलोडिंग का अर्थ है कि एक ही क्लास में अलग-अलग पैरामीटर वाले एक से अधिक कंस्ट्रक्टर हों।

class Test {
int a, b;
Test(int x) {
a = x;
}
Test(int x, int y) {
a = x;
b = y;
}
void display() {
System.out.println("a: " + a + ", b: " + b);
}
public static void main(String[] args) {
Test t1 = new Test(25);
Test t2 = new Test(25, 36);
t1.display();
t2.display();
}
}

🔁 Difference between Constructor and Method (कंस्ट्रक्टर और मेथड में अंतर)

  • A constructor has the same name as the class; a method can have any name.

  • Constructor is automatically called when an object is created; method needs to be called manually.

  • Constructor does not return any value; method may return a value.

  • कंस्ट्रक्टर का नाम क्लास के नाम के समान होता है, जबकि मेथड का नाम कुछ भी हो सकता है।

  • कंस्ट्रक्टर ऑब्जेक्ट बनते ही अपने आप कॉल होता है, जबकि मेथड को कॉल करना पड़ता है।

  • कंस्ट्रक्टर कोई मान वापस नहीं करता, जबकि मेथड वापस कर सकता है।


🔂 Copy Constructor Alternative in Java (कॉपी कंस्ट्रक्टर का विकल्प)

Java doesn't support copy constructors directly, but we can achieve the same using parameterized constructors that accept an object.

जावा में कॉपी कंस्ट्रक्टर नहीं होता, लेकिन हम ऑब्जेक्ट को पैरामीटर के रूप में पास करके समान प्रभाव प्राप्त कर सकते हैं।

class Test {
int a;
Test(int x) {
a = x;
}
Test(Test t) {
a = t.a;
}
void display() {
System.out.println("a: " + a);
}
public static void main(String[] args) {
Test t1 = new Test(50);
Test t2 = new Test(t1);
t2.display();
}
}

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध