Theory
એક shelf, બે tax rates
ShopKeeper Indian GST ને meet કરે છે. Groceries 5% લઈ જાય છે; ખૂણામાં રહેલો electric kettle 18% લઈ જાય છે. bill ને કોઈ પણ product ને correctly price કરવા ONE routine ની જરૂર છે, till code ની મધ્યમાં categories ના growing If-ladder વગર.
તમે આ shape ને BCA403 માં Person family અને overridden describe() સાથે solve કર્યું હતું. VB.NET માં તેને rebuild કરવાથી તેના new spelling માં ત્રણેય OOP pillars assemble થાય છે, અને તમારા Java reflexes પર એક trap spring કરે છે જે examiners all about જાણે છે.
Theory
3 pillars, VB dictionary
Abstraction: expose કરો કે thing શું કરે છે, hide કરો કે કેવી રીતે. VB નું tool: MustInherit class: template જે કોઈ instantiate નથી કરી શકતું, MustOverride methods સાથે જે pure signatures છે.
Encapsulation: controlled surface પાછળ state ને guard કરો. VB નું tool: Private fields + Properties, તમારો Item.Price guard જે 3 lessons ago હતો, હવે તેના pillar name ને wearing.
Polymorphism: એક call, runtime type દ્વારા many behaviours. VB ના tools: parent method પર Overridable, child પર 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 ' અહીં કોઈ body નથી
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
અહીં fail થતો Java reflex
BCA403 માં, દરેક instance method virtual by default હતી: child માં same signature લખો, overriding just happened.
VB.NET કહે છે no. method final છે જ્યાં સુધી તેની class તેને Overridable declare ન કરે, અને child એ Overrides કહેવું જોઈએ. pair ને forget કરો અને તમે override નથી કર્યું: તમે collided છો (VB hide કરવા Shadows ને demand કરે છે, અને તમને loudly warn કરે છે).
એટલે અહીં polymorphism checklist માં 2 boxes છે જે Java એ તમને ક્યારેય tick કરવા દીધા નહીં. BCA304 ના C++ veterans opt-in ને recognise કરશે: Overridable એ virtual છે friendlier name સાથે.
Quiz
Dim p As New Product() compiler શું કહે છે?
- Fine: p default Name અને Price values મેળવે છે
- Compile error: MustInherit class ને instantiate નથી કરી શકાતી
- Runtime error પહેલી વાર જ્યારે GstRate() call થાય છે
- Fine, પણ 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() ને outright refuse કરે છે. Option C failure ને run time પર push કરે છે, પણ .NET આ statically settle કરે છે, same as BCA403 ના Java ના abstract classes. Option D default body ને invent કરે છે જે MustOverride specifically forbid કરે છે. note શું IS legal અને essential છે: Dim p As Product = New Electronics(): abstract REFERENCE concrete object પર, handle જે polymorphism hold કરે છે.
Think first
બંને bills ને trace કરો
Rice (Grocery) 100 પર અને Kettle (Electronics) 100 પર બંને SAME PriceWithGst() માંથી pass થાય છે. બંને results ને compute કરો અને explain કરો કે એક function એ બે answers કેવી રીતે produce કર્યા, tap કરતા પહેલા.
Show the answer
Rice: 100 × (1 + 0.05) = 105. Kettle: 100 × (1 + 0.18) = 118. PriceWithGst એકવાર, Product માં, lives કરે છે, પણ તેનો GstRate() call object ની RUNTIME type દ્વારા dispatched થાય છે: Grocery 0.05 answer કરે છે, Electronics 0.18 answer કરે છે. એ polymorphism છે જે તેનું keep earn કરે છે: till Product references પર loop કરે છે અને ક્યારેય પૂછતું નથી કે anything શું છે; next year નો new category (say Luxury at 28%) = એક new class, billing code માં zero changes. If-ladder alternative ને દરેક વાર tax law sneezes ત્યારે editing ની જરૂર પડતી.
Watch out
VB clothing માં Pillar traps
Overridable/Overrides ને Missing કરવું: Java-reflex bug; pair વગર, parent references parent method ને run કરે છે અને તમારું "override" unused sulks કરે છે.
body સાથે MustOverride: contradiction, compile નથી થતું; MustInherit classes સામાન્ય implemented methods (PriceWithGst એક છે) contain કરી શકે છે.
wrong pillar ને Naming કરવું: _price ને Property પાછળ hide કરવું એ encapsulation છે, abstraction નહીં; MustInherit Product એ abstraction છે. Exams love પૂછે છે કે which is which: tool answers pillar.
Theory
Sealing, અને road ahead
બે closing keywords family ને complete કરે છે: NotInheritable class ને children સામે seal કરે છે (String આ રીતે sealed છે), અને NotOverridable overrides ની chain ને some level પર stop કરે છે. તમારી પાસે હવે VB નું આખું OOP vocabulary છે except એક piece: unrelated classes વચ્ચે contracts. એ piece, Interface અને તેના own VB twist સાથે Implements clause, એ Unit 4 નો next અને final lesson છે, અને એ BCA403 ની Printable story જેવું feel થશે stricter paperwork સાથે retold.
Summary
Key takeaways
- Abstraction: MustInherit classes (no instantiation) + MustOverride methods (no body): template pillar.
- Encapsulation: guarding Properties પાછળ Private fields: 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 કરે છે.