📘 Module 6: Object Oriented Programming (OOP) Concepts in Java

चलिए अब हम Object-Oriented Programming (OOP) का अगला और बहुत ही ज़रूरी concept Inheritance को detail में, आसान हिंदी में समझते हैं — with real-life example, Java syntax, benefits, types, और diagram।


🔷 Inheritance क्या होता है?

📌 Definition:

जब एक class दूसरी class की properties (variables + methods) को use या extend करती है, तो इसे Inheritance कहा जाता है।

Java में inheritance से हम code reuse कर सकते हैं और classes में एक parent-child relationship बना सकते हैं।


🎯 Real-Life Example:

मान लीजिए आपके पास एक class है Vehicle, जिसमें common features हैं जैसे:

  • speed
  • start()
  • stop()

अब आप Car, Bike, Bus जैसी अलग-अलग classes बनाना चाहते हैं, जो इन features को use कर सकें। तो आप बार-बार वही code नहीं लिखेंगे — बल्कि आप Vehicle class को inherit करेंगे।


🔧 Java में Inheritance कैसे लिखा जाता है?

class Vehicle {
    int speed = 60;

    void start() {
        System.out.println("Vehicle Started");
    }
}

// Child class inheriting from Vehicle
class Car extends Vehicle {
    void horn() {
        System.out.println("Car horn: Beep Beep!");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.start();      // inherited method
        System.out.println(c.speed);  // inherited variable
        c.horn();       // own method
    }
}

📌 Java में extends keyword का use होता है:

class Child extends Parent {
    // Parent class का data यहाँ मिल जाता है
}

✅ Inheritance के फायदे

BenefitExplanation
🔁 Code Reusabilityएक बार लिखा हुआ code बार-बार use कर सकते हैं
🔧 Easy MaintenanceCommon code एक ही जगह maintain होता है
🏷 Logical StructureReal world जैसी class hierarchy बनती है
🔄 Polymorphism possibleOverriding वगैरह inheritance के बिना नहीं हो सकता

🧠 Types of Inheritance in Java

TypeDiagram
Single InheritanceA → B
Multilevel InheritanceA → B → C
Hierarchical InheritanceA → B, A → C

⚠️ Note: Java में Multiple Inheritance (via class) को support नहीं किया जाता (Ambiguity की वजह से) लेकिन इसे interface के ज़रिये achieve किया जाता है।


🔍 Example: Multilevel Inheritance

class Animal {
    void eat() {
        System.out.println("I can eat");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("I can bark");
    }
}

class Puppy extends Dog {
    void weep() {
        System.out.println("I can weep");
    }
}

📛 Access Control: क्या inherit होता है?

ModifierInherit होता है?
public✔ होता है
protected✔ होता है
default✔ (same package)
private❌ inherit नहीं होता

📌 Method Overriding in Inheritance:

जब child class parent class के method को अपने हिसाब से बदलती है, उसे overriding कहते हैं।

class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Bike extends Vehicle {
    void run() {
        System.out.println("Bike is running safely");
    }
}

🧠 Interview Questions:

  1. Inheritance क्यों ज़रूरी है?
  2. Java में multiple inheritance क्यों नहीं है?
  3. Constructor inherit होता है क्या?
  4. Inheritance और composition में क्या फर्क है?
  5. Overriding कब use करना चाहिए?

🧾 Summary Table:

FeatureInheritance
Keywordextends
Base ClassSuper / Parent
Derived ClassSub / Child
Accesspublic, protected, default (private नहीं)
BenefitCode reuse, Structure, Overriding
TypesSingle, Multilevel, Hierarchical

🖼 Text-Based Diagram (Single Inheritance)

   Vehicle (Parent)
      ↑
     Car (Child)