Inheritance and overriding methods In Hindi

Inheritance and overriding methods In Hindi

पीएचपी प्रोग्रामिंग में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) कांसेप्ट में इनहेरिटेंस फीचर्स प्रोग्रामर को एक पैरेंट या रुट क्लास को दूसरे सबक्लास क्लास से पैरामीटर प्रॉपर्टीज़ और मेथड्स को इनहेरिट करने की परमिशन प्रोवाइड करता है। इनहेरिटेंस कांसेप्ट प्रोग्रामर को किसी प्रोग्राम सोर्स कोड को री-यूजेबिलिटी और लार्ज वॉल्यूम सोर्स कोड मेंटेनेंस में हेल्प करता है। यह फीचर्स प्रोग्रामर को पैरेंट क्लास जो की दूसरे क्लास से इनहेरिट करता है, यहाँ इनहेरिट फीचर्स से डेवलप उस क्लास को सबक्लास या व्युत्पन्न क्लास के रूप में जाना जाता है, और इसे जिस मैन क्लास से इनहेरिट किया जा रहा है, उसे पैरेंट क्लास या बेस क्लास के रूप में जाना जाता है।

Inheritance and overriding methods In Hindi

प्रोग्राम में जब कोई व्युत्पन्न क्लास या सबक्लास पैरेंट क्लास से कोई मेथड या पैरामीटर को इनहेरिट करता है, तो यह प्रोसेस एक स्पेसिफिक प्रोग्राम इम्प्लीमेंटेशन प्रोवाइड करने के लिए क्लास मेथड को ओवरराइड कर सकता है। इस प्रोसेस को मेथड ओवरराइडिंग कांसेप्ट के रूप में जाना जाता हैं।

So, let’s get to know the concepts of inheritance and method overriding better in PHP programming.

Inheritance Class Concept in PHP.

पीएचपी प्रोग्राम में इनहेरिटेंस कन्सेप्ट एक व्युत्पन्न क्लास या सबक्लास को पैरेंट या रुट क्लास के प्रॉपर्टीज़ और मेथड्स से इनहेरिट करने की परमिशन प्रोवाइड करता है। यहाँ प्रोग्रामर प्रोग्राम में जरूरत के आधार पर व्युत्पन्न क्लास या सबक्लास इनहेरिटेड प्रॉपर्टीज़ और क्लास मेथड्स को यूज़ और मॉडिफाई कर सकता है।

Inheritance syntax in PHP programming.

class ParentClass {

    // create program source code for parent class

}

class ChildClass extends ParentClass {

    // create program source code for Child class

}

यहाँ इनहेरिटेंस कांसेप्ट में extenses कीवर्ड का यूज़ यह इंडीकेट करने के लिए किया जाता है कि प्रोग्राम में सबक्लास ChildClass या पैरेंट क्लास ParentClass से इनहेरिट हो रही है।

Example of inheritance in PHP programming.

तो चलिए पीएचपी प्रोग्रामिंग में एक इनहेरिटेंस कांसेप्ट का एक प्रोग्राम क्रिएट करे, जिसमे एक चाइल्ड क्लास, रुट क्लास या पैरेंट क्लास से प्रॉपर्टीज और मेथड्स को इनहेरिट किया गया है।

<?php

// here employee declare as Parent class or root class

class employee {

    public $emp_name;

    // here Constructor class method declares

    public function __construct($emp_name) {

        $this->emp_name = $emp_name;

    }

    // here Parent class method creates

    public function emp_info() {

        echo $this->emp_name . ” Employee info\n”;

    }

}

// here Child class inherits from employee class

class hr extends employee {

    // here Child class method invokes

    public function emp_info() {

        echo $this->emp_name . ” Siddhi Deora\n”;

    }

}

// here we Create an instance of the parent employee class

$employee = new employee(“instance of employee”);

$employee->emp_info();  // Result – instance of employee Employee info.

// here we Create an instance of the child class

$hr = new hr(“Hr”);

$hr->emp_info();  // Result – Hr Siddhi Deora

?>

Explanation of inheritance in PHP.

  • यहाँ प्रोग्राम में hr डिक्लेअर एक सबक्लास है, जो रुट में एम्प्लॉई क्लास से इनहेरिट हो रही है।
  • यहाँ hr सबक्लास में hr क्लास में एक मेथड emp_info() डिक्लेअर है, जो एम्प्लॉई क्लास की emp_info() मेथड को ओवरराइड करके स्पेसिफिक बिहैवियर प्रोवाइड  करती है।

Method overriding concept in PHP programming.

पीएचपी प्रोग्रामिंग में मेथड ओवरराइडिंग कांसेप्ट तब परफॉर्म होती है, जब कोई सबक्लास रुट या पैरेंट क्लास से इनहेरिटेंस में रिसीव क्लास मेथड को री-डिफाइन करता है। यह कांसेप्ट सबक्लास मेथड का अपना सेल्फ इम्प्लीमेंटेशन प्रोवाइड करने की परमिशन प्रोवाइड करती है।

Key points about method overriding in PHP classes.

  • मेथड ओवरराइडिंग कांसेप्ट में प्रोग्राम सबक्लास की मेथड का नाम और हसिग्नेचर पैरामीटर रुट या पैरेंट क्लास मेथड के इक्वल या कम्पेटिबल होना चाहिए।
  • मेथड ओवरराइडिंग कांसेप्ट में क्लास एक्सेस मॉडिफायर पब्लिक, प्राइवेट, प्रोटेक्टेड, पैरेंट क्लास की मेथड के बराबर होना चाहिए।

Example of method overriding in PHP programming.

<?php

// here computer Parent class created

class computer {

    public function select_choice() {

        echo “select your computer \n”;

    }

}

// here laptop Child class inherits from computer parent class

class laptop extends computer {

    // here we Overriding the select_choice method

    public function select_choice() {

        echo “you select laptop computer\n”;

    }

}

// here we Create an instance of the parent class

$computer = new computer();

$computer->select_choice();  // Result – select your computer

// here we Create an instance of the child class

$laptop = new laptop();

$laptop->select_choice();  // Result – you select laptop computer

?>

Explanation of PHP method overriding.

इस प्रोग्राम में laptop क्लास, computer क्लास के select_choice() मेथड को ओवरराइड करता है। और पैरेंट क्लास का select_choice() “select your computer” मैसेज को कंसोल स्क्रीन में प्रिंट करता है. लेकिन laptop क्लास में ओवरराइड की गई क्लास मेथड “you select laptop computer” मैसेज को प्रिंट करती है।

Access modifiers with method overriding in PHP programming.

पीएचपी प्रोग्रामिंग में किसी यूजर डिफाइन क्लास मेथड को ओवरराइड करते समय, क्लास एक्सेस मॉडिफ़ायर चाइल्ड सबक्लास में इक्वल या अधिक कम्पेटिबल अवश्य होना चाहिए। यहाँ क्लास को पब्लिक को पब्लिक या प्रोटेक्टेड मेथड द्वारा ओवरराइड किया जा सकता है। जिसमे प्रोटेक्टेड कीवर्ड को केवल प्रोटेक्टेड या पब्लिक कीवर्ड के द्वारा ओवरराइड किया जा सकता है। याद रहे, किसी क्लास में प्राइवेट कीवर्ड मेथड को ओवरराइड नहीं किया जा सकता है।

Example of overriding with the correct access modifier in PHP.

<?php

// here we create a computer as Parent class

class computer {

    // here Private class method created

    private function your_selection() {

        echo “Select your computer device \n”;

    }

    // here Public class method created

    public function select_choice() {

        $this->your_selection();

    }

}

// here laptop Child class inherits from computer

class laptop extends computer {

    // here it tries to Attempt to override private class method (error)

    // here we use public function your_selection() {

    //     echo “you select laptop \n”;

    // }

    // here it uses Correct way to Overriding the public class method

    public function select_choice() {

        echo “you select laptop \n”;

    }

}

$laptop = new laptop();

$laptop->select_choice();  // Result – you select laptop

?>

Explanation of overriding with the correct access modifier.

यहाँ प्रोग्राम में पैरेंट क्लास computer में एक प्राइवेट मेथड में डिक्लेअर your_selection() मेथड है, जिसे चाइल्ड क्लास laptop द्वारा डायरेक्ट ओवरराइड नहीं किया जा सकता है। यहाँ जबकि, चाइल्ड क्लास अपना सेल्फ इम्प्लीमेंटेशन प्रोवाइड करने के लिए पब्लिक select_choice() मेथड को ओवरराइड कर सकता है।

parent keyword in PHP programming.

पीएचपी प्रोग्रामिंग में पैरेंट कीवर्ड प्रोग्रामर को पैरेंट क्लास से मेथड्स को कॉल करने या क्लास प्रॉपर्टीज़ को एक्सेस करने की परमिशन प्रोवाइड करता है, चाहे वे चाइल्ड क्लास में ओवरराइड हों।

Example of using parent to call parent methods in a PHP class.

<?php

// here computer Parent class created

class computer {

    public function select_choice() {

        echo “you can select desire computer \n”;

    }

}

// here laptop Child class inherits from computer

class laptop extends computer {

    public function select_choice() {

        // here it Calling the parent class’s select_choice method

        parent::select_choice();  // here it Calls computer select_choice method

        echo “you select hp laptop \n”;  // now it Adds its own class behaviour

    }

}

$laptop = new Laptop();

$laptop->select_choice();

// Result –

// you can select desire computer

// you select hp laptop

?>

Explanation of how to call parent methods in PHP.

इस प्रोग्राम में laptop क्लास, select_choice() मेथड को ओवरराइड करता है। यहाँ ओवरराइड की गई क्लास मेथड में हम laptop स्पेसिफिक बिहैवियर को ऐड करने से पहले पैरेंट क्लास के select_choice() क्लास मेथड को कॉल करने के लिए parent::select_choice() मेथड को यूज़ करते हैं।

Final methods and classes in PHP.

पीएचपी प्रोग्रामिंग में final एक रिजर्व्ड कीवर्ड है, फाइनल कीवर्ड का यूज़ पीएचपी क्लास में इनहेरिटेंस या ओवरराइडिंग मेथड को स्टॉप करने में किया जा सकता है, पीएचपी क्लास में एक फाइनल मेथड को डिक्लेअर सबक्लास द्वारा ओवरराइड नहीं किया जा सकता है। याद रहे, किसी फाइनल क्लास को क्लास में एक्सटेंड नहीं किया जा सकता है. इसका मतलब है की, मौजूदा क्लास प्रोग्राम में कोई भी सबक्लास इससे इनहेरिट नहीं हो सकती है।

Example of using final in a PHP program.

<?php

// here we craete Final class which cannot be extended as inherit

final class computer {

    public function select_choice() {

        echo “Select your desire computer \n”;

    }

}

// here we try to Attempt to extend a final class will generate an error

// here class laptop extends to computer { }  // Display Fatal error – Cannot inherit from final class computer

$computer = new computer();

$computer->select_choice();  // Result – Select your desire computer

?>

Explanation of using final in PHP.

इस प्रोग्राम में कंप्यूटर एक फ़ाइनल कीवर्ड डिक्लेअर क्लास है, इस कारण इसे किसी अन्य क्लास द्वारा एक्सटेंड नहीं किया जा सकता है।

A summary of inheritance and method overriding in PHP programming.

  • पीएचपी प्रोग्रामिंग में इनहेरिटेंस कांसेप्ट एक सबक्लास को पैरेंट क्लास से प्रॉपर्टीज़ और क्लास मेथड्स को इनहेरिट करने की परमिशन प्रोवाइड करता है।
  • पीएचपी प्रोग्रामिंग में मेथड ओवरराइडिंग एक सबक्लास को पैरेंट क्लास से इनहेरिट की गई क्लास मेथड को फिर से क्लास में डिफाइन करने की परमिशन प्रोवाइड करता है।
  • पीएचपी प्रोग्रामिंग में ओवरराइड की गई क्लास मेथड के अंदर पैरेंट क्लास से क्लास मेथड्स को कॉल करने के लिए पैरेंट कीवर्ड का यूज़ करते है।
  • पीएचपी प्रोग्रामिंग में क्लास एक्सेस मॉडिफायर पब्लिक, प्रोटेक्टेड, प्राइवेट मेथड्स और प्रॉपर्टीज की विजिबिलिटी को कण्ट्रोल करते हैं, और क्लास मेथड्स को ओवरराइड करते समय, चाइल्ड क्लास की मेथड का एक्सेस लेवल इक्वल या अधिक कम्पेटिबल होना चाहिए।
  • याद रहे, क्लास में फाइनल मेथड मेथड को ओवरराइड नहीं किया जा सकता है, इसी के साथ फाइनल क्लास को प्रोग्राम में एक्सटेंड नहीं किया जा सकता है।

Leave a Reply