Inheritance


Inheritance বা উত্তরাধিকার (উত্তরাধিকার সূত্রে প্রাপ্তি) হলো Object-Oriented Programming (OOP)-এর একটি গুরুত্বপূর্ণ ধারণা, যার মাধ্যমে একটি ক্লাস অন্য একটি ক্লাসের fields (ডাটা) এবং methods (আচরণ) ব্যবহার করতে পারে। অর্থাৎ, একটি ক্লাস আরেকটি ক্লাসের বৈশিষ্ট্য অর্জন করতে পারে — যেমন, সন্তান তার পিতামাতার বৈশিষ্ট্য উত্তরাধিকার সূত্রে পায়।


ইনহেরিটেন্স কীভাবে কাজ করে?

Java-তে Inheritance দুইভাবে বাস্তবায়ন করা যায়

Class inheritance: এক ক্লাস অন্য ক্লাসের বৈশিষ্ট্য extends কীওয়ার্ড ব্যবহার করে গ্রহণ করে।

Interface inheritance: একটি ক্লাস কোনো interface-কে implements করে তার abstract মেথডগুলোর বাস্তবায়ন দেয়।


উদাহরণ – Class Inheritance

class Animal {
    void eat() {
        System.out.println("Animal is eating...");
    }
}

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


এখন যদি আমরা নিচের কোড চালাই 

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();   // Animal ক্লাসের মেথড
        dog.bark();  // Dog ক্লাসের মেথড
    }
}


আউটপুট:

Animal is eating...
Dog is barking...

এখানে Dog ক্লাস Animal ক্লাসকে extends করেছে। তাই Dog অবজেক্ট Animal-এর eat() মেথডটিও ব্যবহার করতে পারছে।


কেন Inheritance দরকার?

Inheritance ব্যবহারের মাধ্যমে আমরা কোডকে পুনঃব্যবহারযোগ্য (Reusable) করতে পারি।
একই ফাংশনালিটি বারবার লেখার প্রয়োজন পড়ে না।
এছাড়া এটি আমাদের কোডকে করে তোলে আরও সংগঠিত (structured) এবং পরিষ্কার (clean)


Inheritance এর সুবিধা

সুবিধা ব্যাখ্যা
Code Reusability প্যারেন্ট ক্লাসের কোড বারবার না লিখে সরাসরি ব্যবহার করা যায়।
Hierarchical Structure ক্লাসগুলোকে বৈশিষ্ট্য অনুযায়ী ধাপে ধাপে তৈরি করা যায়।
Polymorphism বাস্তবায়ন সহজ হয় প্যারেন্ট টাইপ দিয়ে চাইল্ড অবজেক্টকে রেফার করা সম্ভব হয়।
Maintenance সহজ হয় প্যারেন্ট ক্লাসে পরিবর্তন করলে সব চাইল্ড ক্লাসে সেই আপডেট প্রভাবিত হয়।


Interface Inheritance উদাহরণ

interface Animal {
    void eat();
}

class Cat implements Animal {
    public void eat() {
        System.out.println("Cat eats fish.");
    }
}

এখানে Cat ক্লাসটি Animal ইন্টারফেসকে implements করেছে, এবং তার মধ্যে থাকা eat() মেথডের বাস্তবায়ন দিয়েছে।


Multilevel Inheritance উদাহরণ

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

class Mammal extends Animal {
    void walk() {
        System.out.println("Walking...");
    }
}

class Dog extends Mammal {
    void bark() {
        System.out.println("Barking...");
    }
}


এখন:

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Animal থেকে
        dog.walk(); // Mammal থেকে
        dog.bark(); // Dog থেকে
    }
}


আউটপুট:

Eating...
Walking...
Barking...

এখানে Dog ক্লাস Mammal থেকে এবং Mammal Animal থেকে বৈশিষ্ট্য পেয়েছে।
অর্থাৎ, একাধিক স্তরে (Multi-level) ইনহেরিটেন্স কাজ করছে।


Java-তে Multiple Inheritance কেন নেই?

Java সরাসরি Multiple Class Inheritance সমর্থন করে না। অর্থাৎ, একটি ক্লাস একসাথে একাধিক ক্লাসকে extends করতে পারে না। এর কারণ হলো ambiguity problem — যদি দুই parent ক্লাসে একই মেথড থাকে, child ক্লাস কোনটা ব্যবহার করবে তা নির্ধারণ করা জটিল হয়ে যায়। তবে interfaces এর মাধ্যমে একাধিক উত্তরাধিকার সম্ভব, কারণ সেগুলো কেবল method declaration ধারণ করে।


উদাহরণ – Multiple Interface Inheritance

interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Document implements Printable, Showable {
    public void print() {
        System.out.println("Printing document...");
    }
    public void show() {
        System.out.println("Showing document...");
    }
}

এখানে Document ক্লাস দুটি ইন্টারফেসকেই implement করেছে — ফলে একাধিক আচরণ অর্জন করতে পেরেছে, কোনো দ্বন্দ্ব ছাড়াই।


-----

ইনহেরিটেন্স হচ্ছে এমন একটা মেকানিজম যেখানে একটা ক্লাস আরেকটা ক্লাস থেকে ফিল্ড এবং মেথড একোয়ার করতে পারবে।  জাভাতে দুটা উপায়ে এটা করা যায় 

১।  প্যারেন্ট ক্লাসকে extends করে, এটাকে বলে class inheritance

২।  ইন্টারফেসকে implements করে, এটাকে বলে interface inheritance


সুবিধা 

  • কোডের রিউজাবিলিটি বেড়ে যায় 
  • অবজেক্ট কে বৈশিষ্ট অনুযায়ী ধাপে ধাপে তৈরী করা যায় 
  • এর মাধ্যমে পলিমরফিজম এর ধারণা বাস্তবায়ন করা যায় 


class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }
}

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