Overloading and overriding: concepts, difference and application

Overloading एक ही scope में एक जैसे नाम पर अलग parameter lists वाले कई functions हैं; overriding एक derived class द्वारा बिल्कुल वही signature के साथ एक base function को फिर से define करना है।

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

दो words जो आपको confuse करने के लिए बने हैं

Overloading। Overriding। दो words जो नौ letters share करते हैं, एक ही unit में रहते हैं, और लगभग हर साल एक exam सवाल में साथ दिखते हैं: "Function overloading और function overriding differentiate कीजिए।"

एक viva में इन्हें mix कीजिए और follow-up सवाल जल्दी असहज हो जाते हैं।

अच्छी ख़बर यह है: आप पहले से बिना नाम लिए दोनों इस्तेमाल कर चुके हैं। Till के दो bill() versions? Overloading। Person के discount() को फिर से लिखता Student? Overriding।

Theory

वही counter बनाम family recipe

Overloading एक counter है जो chai, chai(large), chai(large, extra sugar) offer करता है: एक नाम, और cashier आपने क्या माँगा देखकर recipe चुनता है।

Overriding family की franchise branch है जो parent की standard thali recipe लेकर इसे अपने version से बदल देती है, वही dish नाम, वही order slip, अलग kitchen। आपको कौन सी मिलती है यह depend करता है कौन सी branch आपको serve करती है।

Theory

दोनों, formally

Function overloading: एक ही scope में दो या ज़्यादा functions एक नाम share करते हैं पर अपनी parameter list में अलग होते हैं (parameters की संख्या या types)। Compiler arguments match करके select करता है।

double bill(double amt);

double bill(double amt, double disc);

Function overriding: एक derived class एक base-class function को वही नाम AND वही signature से फिर define करती है। Child का version उस child के लिए parent का replace करता है।

class Person { virtual double discount(); };

class Student : public Person { double discount(); };

At a glance

Overloading बनाम overriding, exam table

AspectOverloadingOverriding
SignatureDIFFER होनी चाहिए (params)बिल्कुल MATCH होनी चाहिए
कहाँवही scope / classBase class बनाम derived class
Inheritance चाहिएनहींहाँ
ResolvedCompile timeRun time (virtual के साथ)
Polymorphism familyCompile-time (static)Run-time (dynamic)

Quiz

एक class में, एक student लिखता है: int calc(int a); और double calc(int a); एक overload की उम्मीद में। क्या होता है?

  1. Compile error: सिर्फ़ अलग return type से एक function overload नहीं हो सकता
  2. काम करता है: return types signatures को अलग बनाते हैं
  3. काम करता है, पर सिर्फ़ int version कभी call होता है
  4. calc पहली बार call होने पर runtime error
Show the answer

Compile error: सिर्फ़ अलग return type से एक function overload नहीं हो सकता

Overload resolution सिर्फ़ parameter list देखता है। दोनों functions (int) लेते हैं, तो compiler के लिए वे एक ही function है जो clashing return types के साथ दो बार declared है: एक error। यह सबसे-ज़्यादा-tested overloading fact है। Overload करने के लिए, parameters बदलिए: calc(int) और calc(double) ठीक होंगे।

Think first

इन चार को classify कीजिए

tap करने से पहले हर एक को overloading या overriding label कीजिए:

1. एक class में Item() और Item(char n[], double p)

2. Person के show() को वही signature से फिर define करता Staff

3. एक file में print(int) और print(char)

4. Person से discount() फिर define करता Student

Show the answer

1. Overloading (अलग parameter lists वाले constructors, overloading का everyday application)।

2. Overriding (base बनाम derived, वही signature)।

3. Overloading (वही scope, अलग parameter types)।

4. Overriding (card family का पूरा मुद्दा)।

Reuse करने के लिए quick test: एक जगह अलग params = overloading; एक parent-child pair के पार वही signature = overriding।

Watch out

तीन mark-losers

1. यह दावा करना कि overloading को inheritance चाहिए: नहीं चाहिए; यह एक scope के अंदर रहता है।

2. यह दावा करना कि सिर्फ़ return type से overload हो सकता है: नहीं हो सकता; parameters अलग होने चाहिए।

3. virtual भूल जाना: बिना इसके overriding अभी भी compile होता है, पर एक base-class pointer base version call करेगा (early binding)। Overriding तभी run-time polymorphism बनता है जब base function virtual declared हो।

Theory

हर एक कहाँ अपना काम कमाता है

Wild में overloading: constructors (default + parameterized + copy, एक नाम, तीन forms) और operator overloading (total + item को आपके अपने types के लिए meaningful बनाना)।

Wild में overriding: हर framework जो आप कभी इस्तेमाल करेंगे आपको एक base class देता है और उम्मीद करता है आप इसके hooks override करें: Android का onCreate(), एक game engine का update()। Card family का discount() इसी pattern का miniature है।

Summary

Key takeaways

  • Overloading: वही नाम, अलग parameter list, वही scope, compile-time choice।
  • Overriding: वही नाम, वही signature, base बनाम derived, virtual होने पर run-time choice।
  • अकेले return type से कोई function कभी overload नहीं हो सकता।
  • Overloading को inheritance नहीं चाहिए; overriding इसके बिना exist नहीं कर सकता।
  • Applications: overloaded constructors और operators; discount() जैसे overridden hooks।
  • Memory hook: एक counter, कई recipes बनाम franchise family dish फिर से लिखती है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Polymorphism

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Overloading and overriding: concepts, difference and application · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn