BCA / B.Tech 83 min read

Diffrence Beetween Class & Object in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Java Notes in Hindi | जावा नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Diffrence Beetween Class & Object in Hindi - General Topics
  • • अवलोकन: Quick Answer: Class एक blueprint या template है, जो बताता है कि object में कौन से variables (data) और methods होंगे। Object उसी class का असली instance ह...
  • • मुख्य अवधारणाएं: Class और Object में अंतर, Class vs Object in Hindi, OOP concepts, Object और Class का उपयोग, Object Oriented Programming in Hindi,
Quick Answer: Class एक blueprint या template है, जो बताता है कि object में कौन से variables (data) और methods होंगे। Object उसी class का असली instance है, जो program चलते समय new से बनता है और memory (heap) में अपनी जगह लेता है। एक class से जितने चाहें उतने objects बना सकते हैं।

अगर आपको अभी class और object का basic concept नहीं पता, तो पहले यह note पढ़ लें: Class & Object in Hindi. यहाँ हम सिर्फ दोनों का difference समझेंगे, ताकि exam में comparison वाला question आसानी से लिख सकें।

Real-life Example से समझें

  • घर का नक्शा vs असली घर: Architect एक नक्शा (map) बनाता है। उस नक्शे में कोई रह नहीं सकता। उसी नक्शे से 10 घर बन सकते हैं, और हर घर का रंग, मालिक और address अलग हो सकता है। यहाँ नक्शा = class, हर घर = object।
  • Cookie cutter vs cookies: Cutter एक ही है, पर उससे सैकड़ों cookies बनती हैं। कोई chocolate वाली, कोई vanilla वाली। Cutter खाया नहीं जाता, cookie खाई जाती है। Cutter = class, cookie = object।

मतलब class सिर्फ बताती है कि चीज़ कैसी होगी, और object वो चीज़ है।

Advertisement

Class और Object में अंतर (Comparison Table)

PointClassObject
DefinitionObjects बनाने का blueprint / templateClass का असली instance
Keywordclass keyword से declare होती हैज़्यादातर new keyword से बनता है
EntityLogical entity (सिर्फ design)Physical entity (memory में असली data)
MemoryInstance variables के लिए कोई memory नहीं मिलती। सिर्फ class की जानकारी (metadata, method code, static variables) method area / metaspace में load होती हैहर object को heap में अपनी अलग memory मिलती है, जिसमें उसके instance variables रहते हैं
कितनी बारएक program में एक class एक ही बार declare होती हैएक class से कितने भी objects बना सकते हैं
कब बनता हैCode लिखते समय declare होती है, compile time पर .class file बनती है, और पहली बार use होने पर JVM उसे load करता हैRuntime पर, जब new वाली line चलती है
State (values)Class में fields सिर्फ declare होते हैं, उनकी अपनी value नहीं होती (static fields को छोड़कर)हर object के fields में अपनी अलग values होती हैं
BehaviourMethods define करती हैउन methods को call करता है, अपने data पर
Pass / ReturnClass को value की तरह method में pass नहीं कर सकतेObject का reference method में pass और return कर सकते हैं
DestroyProgram चलते समय class destroy नहीं होती (आम तौर पर)जब कोई reference नहीं बचता, तो garbage collector object को हटा देता है
Exampleclass Student { ... }Student s1 = new Student();
Real-lifeघर का नक्शा, cookie cutterअसली घर, cookies

Java Program: एक Class, दो Objects

class Student {
    // fields (instance variables)
    String name;
    int rollNo;

    // constructor
    Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }

    // method
    void show() {
        System.out.println("Name: " + name + ", Roll No: " + rollNo);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Aman", 101);   // पहला object
        Student s2 = new Student("Priya", 102);  // दूसरा object

        s1.show();
        s2.show();
    }
}

Output

Name: Aman, Roll No: 101
Name: Priya, Roll No: 102

यहाँ Student class एक ही है, पर s1 और s2 दो अलग objects हैं। दोनों के पास अपना name और rollNo है। show() method एक ही है, पर जो object उसे call करता है, उसी का data print होता है।

Stack में Reference, Heap में Object

s1 और s2 खुद object नहीं हैं, ये reference variables हैं। ये main() के अंदर local variables हैं, इसलिए stack में रहते हैं और heap में बने object का address रखते हैं। Heap और stack JVM की memory के हिस्से हैं, इनके बारे में यहाँ पढ़ें: JVM और JRE in Hindi.

   STACK (main method)            HEAP
  +-----------------+       +----------------------+
  | s1  | ref ------+-----> | name   = "Aman"      |
  +-----------------+       | rollNo = 101         |
  | s2  | ref ---+  |       +----------------------+
  +-----------------+
                 |          +----------------------+
                 +--------> | name   = "Priya"     |
                            | rollNo = 102         |
                            +----------------------+

  METHOD AREA / METASPACE:  Student class की जानकारी + show() का code (एक ही copy)

ध्यान दें: show() का code हर object में copy नहीं होता। Method का code class के साथ एक ही बार रहता है, सिर्फ data (fields) हर object का अलग होता है।

Java में Object बनाने के तरीके

तरीकाExampleकब use होता है
new keywordStudent s = new Student("Aman", 101);सबसे common तरीका, 99% cases में यही
clone()Student s2 = (Student) s1.clone();पहले से बने object की copy चाहिए हो। Class को Cloneable implement करना पड़ता है
DeserializationObjectInputStream से readObject()File या network से saved object वापस लाना
ReflectionStudent.class.getDeclaredConstructor(...).newInstance(...)Frameworks (जैसे Spring) में, जब class का नाम runtime पर पता चलता है

new असल में क्या करता है:

  1. Heap में object के लिए memory लेता है और fields को default values देता है (0, null, false)।
  2. Constructor चलाता है, जो fields में असली values डालता है।
  3. उस object का reference return करता है, जो s1 जैसे variable में store हो जाता है।

Note: पुराना Class.newInstance() method Java 9 से deprecated है, इसलिए अब getDeclaredConstructor().newInstance() लिखा जाता है। Constructor के बारे में ज़्यादा जानने के लिए Class & Method in Hindi देखें।

Advertisement

Students की Common Mistakes

1. Static main से बिना object के instance method call करना

public class Test {
    void hello() {
        System.out.println("Hello");
    }
    public static void main(String[] args) {
        hello();   // Error: non-static method hello() cannot be referenced from a static context
    }
}

main() static है, यानी वो class से जुड़ा है, किसी object से नहीं। hello() instance method है, उसे object चाहिए। सही तरीका: new Test().hello(); या hello() को भी static बना दें।

2. Reference variable को ही object समझ लेना

Student s;              // सिर्फ reference declare हुआ, कोई object नहीं बना
Student s1 = new Student("Aman", 101);
Student s2 = s1;        // नया object नहीं बना! दोनों एक ही object को point कर रहे हैं
s2.name = "Ravi";
System.out.println(s1.name);   // Ravi

s2 = s1 लिखने से object copy नहीं होता, सिर्फ address copy होता है। इसलिए s2 से बदलाव करने पर s1 में भी दिखता है। Object तभी बनता है जब new (या ऊपर के बाकी तरीके) चलते हैं।

3. null reference पर method call करना

Student s = null; s.show(); लिखने पर compile तो हो जाता है, पर runtime पर NullPointerException आता है, क्योंकि reference किसी object को point ही नहीं कर रहा।

4. "Class memory नहीं लेती" लिख देना

Exam में ये line आधी-सही है। Class के instance variables के लिए memory नहीं मिलती, पर class की जानकारी (metadata, methods, static variables) JVM के method area / metaspace में load होती है। Answer में यही लिखें।

Class और object OOP की नींव हैं। Inheritance, polymorphism जैसे बाकी concepts के लिए पढ़ें: OOPs Concepts in Java in Hindi.

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Class और object में कोई चार अंतर लिखिए।
  2. Object को class का instance क्यों कहते हैं? एक example दीजिए।
  3. Logical entity और physical entity से क्या मतलब है?
  4. new keyword का काम बताइए।

Long Questions (5–10 marks)

  1. Class और object में अंतर table बनाकर समझाइए। एक Java program भी लिखिए।
  2. Java में object कितने तरीकों से बनाया जा सकता है? new keyword को detail में समझाइए।
  3. Reference variable और object में क्या अंतर है? Stack और heap memory के diagram के साथ समझाइए।

FAQs

क्या बिना object बनाए class का method call कर सकते हैं?

हाँ, अगर method static है। जैसे Math.sqrt(16)। Static method class से जुड़ा होता है, इसलिए उसे object नहीं चाहिए। Non-static (instance) method के लिए object ज़रूरी है।

एक class से कितने objects बना सकते हैं?

जितने चाहें, जब तक heap में memory है। हर object की अपनी अलग copy of instance variables होती है।

क्या object बिना class के बन सकता है?

नहीं। Java में हर object किसी न किसी class का ही instance होता है। Anonymous class में भी पीछे एक class बनती है, बस उसका नाम नहीं होता।

Class को logical और object को physical entity क्यों कहते हैं?

Class सिर्फ design है, उसके instance variables में कोई असली data नहीं होता। Object heap में असली memory लेता है और उसमें असली values होती हैं, इसलिए वो physical है।

s1 और s2 object हैं या reference?

Reference। s1 और s2 वो variables हैं जो heap में बने objects का address रखते हैं। Object खुद heap में होता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Class एक blueprint या template है, जो बताता है कि object में कौन से variables (data) और methods होंगे।
Java Notes in Hindi | जावा नोट्स हिंदी में के पाठ्यक्रम में Diffrence Beetween Class & Object in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Diffrence Beetween Class & Object in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Diffrence Beetween Class & Object in Hindi
BCA || Java 2024 Paper || MDSU Exam Paper
What is Java, Characteristics of Java - Java क्या है और जावा की विशेषताएं
Difference between Java and C++ in Hindi. (Java और C++ में अंतर )
Applications of Java Programming in Hindi
OOPS concepts in Java Hindi
What is JVM and JRE in hindi
Features of JAVA in hindi
What is Java bean in hindi & advantage in Hindi
Difference between Java and JavaScript in Hindi
JIT Compiler in Hindi
What is Control Statement (structure) in Hindi
What is variable in hindi (वेरिएबल क्या होता है?):-
Java URL Class in Hindi
What is Java swing and its methods in Hindi
Operators in Java in Hindi
Class & Method in Hindi
Inheritance in Java in Hindi | जावा में इनहेरिटेंस हिंदी में
instance Operator in Hindi
Token in Hindi
Data Types in Hindi
Constructor in Hindi
Object-Oriented-Design in Hindi
Conditional Operator in Hindi
Constant in Hindi
Reference Variable in Hindi
Type Casting in Hindi
Literals in Hindi
Identifiers in Hindi
Array in Hindi
Anonymous Array in Hindi
Access Modifiers in Hindi
Enum Data type in Hindi
Keywords in Hindi-Java
Super Keywords in Hindi
Static Keywords in Hindi
Final Keywords in Hindi
New Operator in Hindi
Method Types in Hindi
Method Overloading in Hindi
Method Overriding in Hindi
Method Hiding in Hindi
Class & Object in Hindi
Math Class in Hindi
Abstraction in Hindi
Adapter Pattern in Hindi
Package in Hindi
Class & Structure in Java in Hindi
Exception Handling in Hindi