Inheritance in Java जावा में इनहेरिटेंस
Inheritance lets one class acquire properties and behaviors of another class
Inheritance is a fundamental concept of object-oriented programming, where a class (child or subclass) can inherit the properties and behavior of another class (parent or superclass). Java supports inheritance using the extends
keyword. This allows for code reuse, meaning we don’t have to rewrite methods and variables that already exist in the parent class. It works just like children inheriting traits from their parents.
इनहेरिटेंस ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग का एक मूल सिद्धांत है, जिसमें एक क्लास (सबक्लास या चाइल्ड क्लास) किसी अन्य क्लास (सुपर क्लास या पैरेंट क्लास) के गुणों और व्यवहारों को प्राप्त करती है। जावा में extends
कीवर्ड का उपयोग करके इनहेरिटेंस को लागू किया जाता है। यह कोड के पुनः उपयोग (code reuse) को संभव बनाता है।
Syntax:
class SubclassName extends SuperclassName {
// fields and methods}
🔹 Types of Inheritance in Java
Java supports the following types of inheritance:
-
Single Inheritance (एकल वंशानुक्रम)
-
Multilevel Inheritance (बहुस्तरीय वंशानुक्रम)
-
Hierarchical Inheritance (पदानुक्रमित वंशानुक्रम)
-
Hybrid Inheritance (संकर वंशानुक्रम)
1️⃣ Single Inheritance (एकल वंशानुक्रम)
When a single subclass inherits from one superclass.
जब एक सबक्लास केवल एक सुपर क्लास से गुण प्राप्त करती है।
Syntax:
class Superclass {
// statements}class Subclass extends Superclass {// methods and fields}
Example:
class Base {
public void BCM() {System.out.println("Base Class method");}}class Derived extends Base {public void DCM() {System.out.println("Derived Class method");}}public class Test {public static void main(String[] args) {Derived d = new Derived();d.BCM();d.DCM();}}
2️⃣ Multilevel Inheritance (बहुस्तरीय वंशानुक्रम)
Inheritance that occurs across multiple levels of classes.
जब एक क्लास किसी क्लास से इनहेरिट करे और आगे दूसरी क्लास उससे – तो यह multilevel inheritance कहलाता है।
Syntax:
class A { }
class B extends A { }class C extends B { }
Example:
class Base {
public void BCM1() {System.out.println("Base Class method");}}class InterBase extends Base {public void BCM2() {System.out.println("Intermediate Base Class method");}}class Derived extends InterBase {public void DCM() {System.out.println("Derived Class method");}}public class Test {public static void main(String[] args) {Derived d = new Derived();d.BCM1();d.BCM2();d.DCM();}}
3️⃣ Hierarchical Inheritance (पदानुक्रमित वंशानुक्रम)
When multiple subclasses inherit from the same superclass.
जब एक ही सुपर क्लास से दो या अधिक क्लासेस गुण प्राप्त करें।
Syntax:
class A { }
class B extends A { }class C extends A { }
Example:
class Base {
public void BCM() {System.out.println("Base Class method");}}class Derived1 extends Base {public void DCM1() {System.out.println("Derived Class1 method");}}class Derived2 extends Base {public void DCM2() {System.out.println("Derived Class2 method");}}public class Test {public static void main(String[] args) {Derived1 d1 = new Derived1();d1.BCM();d1.DCM1();Derived2 d2 = new Derived2();d2.BCM();d2.DCM2();}}
4️⃣ Hybrid Inheritance (संकर वंशानुक्रम)
It is a combination of two or more types of inheritance.
जब एक प्रोग्राम में एक से अधिक प्रकार की इनहेरिटेंस (single, multilevel, hierarchical) का प्रयोग हो तो उसे hybrid inheritance कहते हैं।
Example:
class Base {
public void BCM() {System.out.println("Base Class method");}}class Derived1 extends Base {public void DCM1() {System.out.println("Derived Class1 method");}}class Derived2 extends Base {public void DCM2() {System.out.println("Derived Class2 method");}}class Derived3 extends Derived2 {public void DCM3() {System.out.println("Derived Class3 method");}}public class Test {public static void main(String[] args) {Derived1 d1 = new Derived1();d1.BCM();d1.DCM1();Derived3 d3 = new Derived3();d3.BCM();d3.DCM2();d3.DCM3();}}
Note:
Java does not support multiple inheritance using classes directly to avoid ambiguity (diamond problem). Instead, it uses interfaces to achieve multiple inheritance functionality.
जावा में मल्टीपल इनहेरिटेंस को क्लासेस के माध्यम से सपोर्ट नहीं किया जाता, बल्कि इसके लिए इंटरफेस का प्रयोग किया जाता है।
Comments
Post a Comment