Sunday, September 22, 2019

What is inheritance ?

අපි අද කතා කරන්න යන්නේ OOP වල ප්‍රධාන concept එකක් වෙන inheritance කියන්නේ මොකද්ද කියලා ?

සරලව කිව්වොත් inheritance කියලා කියන්නේ එක class එකක තියන දේවල් ටිකක් තවත්  class එකකට ගන්න ක්‍රියාවලිය. අපි මේ කතාව example එකකින් බලමු.
Image result for nokia old  phone images(png)Image result for smart nokia   phone images
මුල්ම image එකේ තියෙන්නේ පරණ Nokia  phone එකක්. අන්න  ඒ phone  එකේ තියන  features අරගෙන තවදුටරත් වැඩි දියුණු කරලා Nokia Smart phone එකක් හදනවා.ඒ කියන්නෙ හරියට එක පරම්පරාවක තියන දෙයක් තවත් පරම්පරවකට දෙනවා. ඒ වගේ දෙයක් තමා inheritance වලදි වෙන්නේ.


මෙතනදි Smart phone එක  තමා, පරණ Nokia  phone එකෙන් inherit වෙන්නේ.Smart phone හැදිලා තියෙන්නේ  පරණ Nokia  phone එකේ features එකතු වෙලා.


ඊළඟට අපි බලමු  inheritance වල තියන super class & sub class කියන්නේ මොනවද  කියලා?

අපි ගත්ත phone example එකට අනුව පරණ Nokia  phone එක තමා super class එක, Smart phone එක  තමා sub class එක.
Image result for nokia old  phone images(png)Image result for smart nokia   phone imagesSuper class                        Subclass



  • ඊළඟට අපි බලමු මේ phones දෙකේ තියෙන ගතිලක්ෂණ ,කාර්යයන් මොනවද කියලා?


Super class(Old Nokia phone)
name
price                                                
call( )
SMS( )


  • Sub class  එක හදද්දි  අලුත් features වගේම super class එකේ තිබුණ ගති ලක්ෂණ ,කාර්යයන් එකතු වෙනවා. 

Sub class(Smart Nokia phone)
name
price                                                

call( )
SMS( )
camera( )
Email( )
Bluetooth ( )

දැන්  පොඩි  idea එකක් ආවා නේද?  inheritance කියන එක ඕන  වුණේ කලින් හදපු දෙයක් පාවිචි කරලා,ඒක වැඩි දියුණු කරලා තවත් අලුත් දෙයක් හදන්න කියලා.

දැන් අපි බලමු coding එක්ක කොහොමද inheritance ක්‍රියාවලිය වෙන්නේ කියලා ?
  1. // code for OldPhone - (Super class)
  2. class OldPhone{
  3. String name;
  4. int price;
  5. void call( )
  6. {
  7. }
  8. void sms( )
  9. {
  10. }
  11.  }
  12.  
  13.  
  14.  //code for SmartPhone - (Sub class)
  15.  class SmartPhone extends OldPhone{
  16.  
  17. void camera( )
  18. {
  19. }
  20. void Email( )
  21. {
  22. }
  23. void Bluetooth ( )
  24. {
  25. }
  26.  }

මේකේ  code line no :2 - 14 වන තෙක්  තියෙන්නේ  Old phone(Super class) එකට අදාළ coding part එක.


ඊළඟට code line no :18 - 33 දක්වා  තියෙන්නේ Smart phone(sub class) එකට අදාළ coding part එක. ඒකේ " class SmartPhone extends OldPhone " (code line no 18) කියලා තියන code එකෙන් කියවෙන්නේ  Old phone එකේ තිබුණ දේවල්  Smart phone එකට inherit කරන්න කියලා. එතකොට වෙන්නේ Old phone එකේ තිබ්බ හැම feature එකක්ම Smart phone එකට add වෙනවා. ඊට අමතරව  camera,Email, Bluetooth වගේ අලුත් facilities එකතු කරනවා.මේ ක්‍රියාවලිය තමයි inheritance කියලා කියන්නේ.

class SmartPhone extends OldPhone " . මෙතන "extends" කියන්නේ  keyword එකක්. 

පොඩ්ඩක් Supper class එකේ  code එක බලන්න.Super class එකේ තියෙන්නේ common data. Sub class එකේ  specific data තියනවා.

මේ කිව්ව දේවල් එක්ක inheritance කියන්නේ මොකද්ද කියලා කියන්න පුළුවන්ද? inheritance කියන්නේ එක class එකක තියන පොදු දේවල් ටිකක් තව class එකකට අරගෙන ඒ  class එකට විශේෂ වුණ දේවල් ටික දාලා පහසුකම් වැඩි අලුත් class එක්ක හදන  ක්‍රියාවලිය.

එසේ  නම් OOP වල ප්‍රධාන concept එකක් වෙන inheritance ගැන තමා මම අද ඔයාලට තොරතුරු අරන් අවේ. මම ලඟදීම මේ වගේ වැදගත් වෙන තොරතුරු ටිකක් අරන් එන්න බලාපොරොත්තු වෙනවා.දිගටම Blog එකත් එක්ක ඉන්න කියලා ආරාධනා කරනවා.


75 comments:

  1. Good ගොඩක් හොඳයි👍

    ReplyDelete
  2. Congratulations..!!great work...👍👍

    ReplyDelete
  3. Suuper keep it up❤️❤️

    ReplyDelete
  4. Niyamai digatama karagena yanna apita godaaak dewal igena ganna aeki meken...

    ReplyDelete
  5. Great explanation with examples. Keep it up.

    ReplyDelete
  6. Super 👍👍🤝🤝🔥🔥🔥

    ReplyDelete
  7. නියමයි 👌 👌👌💪💜️

    ReplyDelete
  8. Niyamai..❤️ Digatama karagena yanna..
    Good luck��

    ReplyDelete
  9. Great work 👍👍👍👍👍👍👍👍❤

    ReplyDelete