Abstraction, Encapsulation & Polymorphism

VB की spelling में 3 pillars: abstraction के लिए MustInherit/MustOverride, encapsulation के लिए Properties के पीछे Private fields, और एक polymorphism जिसमें, Java के उलट, आपको खुद opt in करना पड़ता है, Overridable/Overrides से।

11 min read · 9 cards · 2 checks

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


Theory

एक Shelf, दो Tax Rates

ShopKeeper Indian GST से मिलता है। Groceries 5% रखते हैं; कोने वाला electric kettle 18% रखता है। Bill को ONE routine चाहिए जो किसी भी product को correctly price करे, till code के बीच में categories की एक बढ़ती हुई If-ladder के बिना।

आपने यह shape BCA403 में एक Person family और एक overridden describe() के साथ solve किया था। इसे VB.NET में rebuild करना सारे 3 OOP pillars को इनकी नई spelling में assemble करता है, और आपके Java reflexes पर एक trap छोड़ता है जिसे examiners सब जानते हैं।

Theory

3 Pillars, VB Dictionary

Abstraction: expose कीजिए कि चीज़ क्या करती है, hide कीजिए कैसे। VB का tool: एक MustInherit class: एक template जिसे कोई instantiate नहीं कर सकता, MustOverride methods के साथ जो pure signatures हैं।

Encapsulation: state को एक controlled surface के पीछे guard कीजिए। VB का tool: Private fields + Properties, आपका Item.Price guard 3 lessons पहले से, अब अपने pillar का नाम पहने हुए।

Polymorphism: एक call, runtime type से कई behaviours। VB के tools: parent method पर Overridable, child के method पर Overrides।

Practical

Polymorphic GST वाली Product Family

Public MustInherit Class Product        ' abstract: New Product() illegal
    Public Name As String
    Public Price As Decimal

    Public MustOverride Function GstRate() As Decimal   ' no body here

    Public Function PriceWithGst() As Decimal
        Return Price * (1 + GstRate())  ' polymorphic call
    End Function
End Class

Public Class Grocery
    Inherits Product
    Public Overrides Function GstRate() As Decimal
        Return 0.05D                    ' 5%
    End Function
End Class

Public Class Electronics
    Inherits Product
    Public Overrides Function GstRate() As Decimal
        Return 0.18D                    ' 18%
    End Function
End Class

' Dim p As Product = New Electronics With {.Name = "Kettle", .Price = 100D}
' MsgBox(p.PriceWithGst())   ' 118: the Electronics rate answered

Theory

Java Reflex जो यहाँ Fail होता है

BCA403 में, हर instance method default से virtual था: एक child में same signature लिखिए, overriding बस अपने आप हो जाता था।

VB.NET मना करता है। एक method final होता है जब तक इसकी class इसे Overridable declare न करे, और child को Overrides कहना ही होगा। यह pair भूल जाइए और आपने override नहीं किया: आप collide हुए हैं (VB को hide करने के लिए Shadows चाहिए, और यह ज़ोर से warn करता है)।

तो यहाँ polymorphism की checklist में 2 boxes हैं जो Java ने आपसे कभी tick नहीं करवाए। BCA304 के C++ veterans इस opt-in को पहचान लेंगे: Overridable, virtual ही है, बस एक friendlier नाम के साथ।

Quiz

Dim p As New Product() Compiler क्या कहता है?

  1. ठीक है: p को default Name और Price values मिलती हैं
  2. Compile error: एक MustInherit class instantiate नहीं हो सकती
  3. Runtime error, पहली बार जब GstRate() call होता है
  4. ठीक है, पर GstRate() 0 return करता है जब तक कोई child इसे override न करे
Show the answer

Compile error: एक MustInherit class instantiate नहीं हो सकती

MustInherit का मतलब है template-only: Product inherit होने के लिए exist करता है, और इसके MustOverride GstRate का कोई body नहीं है, तो एक bare Product PriceWithGst का answer कभी नहीं दे पाता। इसलिए compiler New Product() को सीधा refuse कर देता है। Option C failure को run time तक push करता है, पर .NET इसे statically settle करता है, BCA403 के Java abstract classes की तरह। Option D एक default body invent करता है जिसे MustOverride specifically मना करता है। ध्यान दीजिए क्या legal AND essential है: Dim p As Product = New Electronics(): एक concrete object की एक abstract REFERENCE, वह handle जो polymorphism holds करता है।

Think first

दोनों Bills Trace कीजिए

Rice (Grocery) 100 पर और एक Kettle (Electronics) 100 पर, दोनों SAME PriceWithGst() से गुज़रते हैं। Tap करने से पहले दोनों results compute कीजिए और explain कीजिए एक function ने दो answers कैसे दिए।

Show the answer

Rice: 100 × (1 + 0.05) = 105। Kettle: 100 × (1 + 0.18) = 118। PriceWithGst एक बार रहता है, Product में, पर इसकी GstRate() call object के RUNTIME type से dispatch होती है: Grocery 0.05 answer देता है, Electronics 0.18। यही polymorphism अपना काम कमा रहा है: till Product references पर loop करता है और कभी नहीं पूछता कि कोई चीज़ क्या है; अगले साल एक नई category (मान लीजिए 28% पर Luxury) = एक नई class, billing code में zero changes। If-ladder वाला alternative हर बार tax law बदलने पर editing माँगता।

Watch out

VB के कपड़ों में Pillar Traps

Missing Overridable/Overrides: वह Java-reflex bug; बिना उस pair के, parent references parent method run करते हैं और आपका "override" बिना इस्तेमाल के पड़ा रहता है।

Body वाला MustOverride: एक contradiction, compile नहीं होता; MustInherit classes normal implemented methods भी CONTAIN कर सकती हैं (PriceWithGst एक है)।

ग़लत pillar को नाम देना: _price को एक Property के पीछे hide करना encapsulation है, abstraction नहीं; MustInherit Product abstraction है। Exams पूछना पसंद करते हैं कौन सा कौन है: tool pillar का answer देता है।

Theory

Sealing, और आगे का Road

दो closing keywords family पूरी करते हैं: NotInheritable एक class को children के against seal करता है (String इसी तरह sealed है), और NotOverridable overrides की एक chain को किसी level पर रोक देता है। अब आपके पास VB की पूरी OOP vocabulary है सिवाय एक piece के: unrelated classes के आर-पार contracts। वह piece, Interface और इसका Implements clause अपने VB twist के साथ, Unit 4 का अगला और आख़िरी lesson है, और यह BCA403 की Printable story को stricter paperwork के साथ retold जैसा feel होगा।

Summary

Key takeaways

  • Abstraction: MustInherit classes (कोई instantiation नहीं) + MustOverride methods (कोई body नहीं): template pillar।
  • Encapsulation: Private fields, guarding Properties के पीछे: doorman pillar।
  • Polymorphism: parent reference, child behaviour, Overridable + Overrides के ज़रिए।
  • VB methods default से virtual नहीं हैं: दोनों keywords compulsory हैं, Java के उलट, C++ के virtual जैसे।
  • Traced GST: एक PriceWithGst, Grocery (5%) को 105 और Electronics (18%) को 118 देता है।
  • NotInheritable classes को seal करता है; NotOverridable methods को seal करता है।
  • Memory hook: MustInherit templates बनाता है, Properties guard करती हैं, Overridable opt in करता है।

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 Object Oriented Programming

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

Abstraction, Encapsulation & Polymorphism · .NET Programming · Gri-Learn