The SIGMA

Method Overriding & super keyword

Reading Time: 6 minutes


කොහොමද යාලුවනේ! ඔන්න මම අදත් ආවා object oriented concept වල තියෙන අලුත්ම කොටසක් ඇරගෙන. අලුත් පාඩම්ක් කිව්වට අලුත්ම එකක් නෙවෙයි.ඔයාලා අහලා තියෙන ටිකක් මතක තියෙන පාඩම්ක් .ඒ තමයි method overriding ගැන. method overriding ගැන කලින් පාඩමක කතා කළා මත්කයි නේද? එතනදි මම කිව්වා method overriding වැඩිපුරම පාවිච්චි වෙන්නේ inheritance වලදී කියලා .inheritance පාඩමේදි method overriding ගැන අපි කතා කතාකරන්න ඉතුරු කළා මත්ක ඇති නේද.. අන්න ඒ ඉතුරු කරපු කොටස ගැන කතා කරන්න තමයි මම මේ පාඩම් වෙන් කරන්නේ.

හරි අපි method overriding ගැන කතා කරන්න ඉස්සරින් මම පොඩ්ඩක් කලින් පාඩම පොඩ්ඩක් මතක් කරන්නම්.කලින් පාඩමේදි අපි කතා කලේ object oriented concept එකේ තියන inheritance කියන feature එක ගැනයි. අපේ කලින් පාඩම බැලුවේ නැතිනම් අද පාඩම තේරුම් ගන්න නම් ටිකක් අමාරු වෙයි. ඒ නිසා කලින් පාඩම ඉක්මනින් ගිහින් බලලා එන්නකෝ. කලින් පාඩමේදි අපේ අපි කතා කළා inheritance කියන්නේ මොකක්ද කියලා. ඒ වගේම inheritance භාවිතා වුණු උදාහරණ දෙකකුත් කතා කළා . ඇත්තටම යාලුවනේ මොකද්ද අපි inheritance කියල කිව්වෙ. inheritance කියලා අපි කිව්වේ එක class එකක තියෙන method සහ attributes තවත් ක්ලාස් එකකට inherit කරලා කලින් class එකට වඩා දියුණු class එකක් සෑදීමයි.inherit කරපු class එකට අපි කිව්වා super class එක කියලා. inheritance කරල හදපු අලුත් ක්ලාස් එකට අපි කිව්වා කිව්වා sub class එක කියලා. class දෙකක් inherit වෙලා තියනවා කියලා පෙන්වීම්ට sub class එකේ ඉඳලා super class එකට ඊතලයක් ඇඳලා පෙන්නුවා මෙන්න මේ විදිහට.

හරි යලුවනේ මතක් උන නේද inheritance කියන්නේ මොකක්ද කියලා . එහෙනම් අපි බලමු අද පාඩම ගැන.කලින් කිව්ව විදියට අද පාඩම වෙන්නෙ method overriding.ඔන්න යාලුවනේ මේ පාඩම method overloading පාඩමත් එක්ක පටලවා ගන්නනම් එපා.method overloading පාඩමේදී කිව්ව විදියට method overriding කියලා අපි කිව්වේ එක class එකක තියන method එකක් අපිට ඕනි විදියට එකේ body එක වෙනස් කරලා ප්‍රයෝජනයට ගන්න එකටයි.method overriding වැඩිපුරම භාවිතා වෙන්නෙ inheritance වලදි කියලත් අපි ඒ පාඩමේදී කිව්වා මතක ඇති නේද හැමෝටම.එහෙනන් අපි බලමු කොහොමද method overriding , inheritance වලදී භාවිතා වෙන්නේ කියලා.

හරි යාලුවනේ ඔයාලා දන්නවනෙ දැන් super class එකකින් sub class එකක් inherit කරන්නෙ අපි extends කියන keyword එක පාවිච්චි කරල කියලා.මේ විදියට inherit කරද්දි super class එකේ තියන privet නොවන attributes සහ methods සියල්ලක්ම sub class එකට එනවා කියලත් ඔයාලා දන්නවා.ඔන්න ඔය කරුණු ටිකත් ඔලුවේ තියාගෙන පෙඩ්ඩක් මම මේ කියන උදාහරණය බලන්නකෝ.

හරි මම හදනවා class එකක් phone කියලා.ඒකේ attributes විදියට මම ගන්නවා color එක හා price එක.method එකක් විදියට මම ගන්නවා call කියන එක. තවත් class එකක් මම හදනවා SmartPhone කියලා.ඒකේ attributes විදියට මම ගන්නවා color එක, price එක හා storage එක .methods විදියට මම ගන්නවා call හා takePhoto කියලා methods දෙකකුත්.දැන් ඔයාලට හොදට බැලුවොත් පේනවා මේ class දෙකටම පොදු attributes හා methods තියනවා කියලා..දැන් ඔයාලට පොඩී අදහසක් එනවා නේද එක class එකක් අනික් class එකෙන් inherit කලොත් හොදයි කියලා.හරි එහෙනම් අපි inherit කරමූ.

class Phone{
      String color;
      int price;

      void call(){
           System.out.println("This phone can  take audio calls only ! ");
      }
}

class SmartPhone extends Phone{
     String storage;

     void takePhoto(){
           System.out.println("This phone can take quality photos ! ");
     }
}

හරි ඔන්න ඔහොම නේද යාලුවනේ SmartPhone කියන class එක phone කියන class එකින් inherit කලාම එන්නේ.මොකක්ද එතකොට අපේ super class එකයි sub class එකයි වෙන්නේ? අපේ super class එක වෙන්නේ Phone class එක.sub class එක වෙන්නෙ SmartPhone කියන class එකයි.inherit කිරීමක් කරපු නිසා super class එකේ තිබුනු attributes සහ methods ඔක්කොම sub class එකට ආවා නේද..හරි යාලුවනේ දැන් මං කියන දේ හොදට සියුම්ව තේරුම් ගන්නකෝ.හරි ඔයා හිතන්න දැන් Phone කියන class එකෙන් හදන object එකකට පුලුවන් call කියන method එකට කතා කලාම audio call විතරයි ගන්න පුලුවන් කියලා.නමුත් SmartPhone කියන class එකෙන් හදන object එකකට තියන call කියන method එකට කතා කලාම audio and video call කියන දෙකම ගන්න පුලුවන් කියලා හිතන්න.( SmartPhone කියන class එක ඇතුලෙ අපිට පේන්න call කියලා method එකක් නැති වුනාට inhetance කරලා තියන නිසා super class එකේ තියන call කියන method එක SmartPhone කියන class එකට ඇවිත් තියෙන්නේ. )දැන් අපි Phone කියන class එකෙයි SmartPhone කියන class එකෙයි object දෙකක් හදලා call කියන method එකට කතා කරමු.

class Phone{
      String color;
      int price;

      void call(){
           System.out.println("This phone can  take audio calls only ! ");
      }
}

class SmartPhone extends Phone{
     String storage;

     void takePhoto(){
           System.out.println("This phone can take quality photos ! ");
     }
}

class CreatingObjects{
      public static void main(String a[]){

             Phone phone1=new Phone();
             phone1.call();
      }
}

output එක දිහාත් හොදට බලන්නකෝ.මේ තියෙන්නෙ Phone කියන class එකින් හදපු object එකේ call method එකට කතා කලාම ලැබෙන output එක.

මේ තියෙන්නෙ SmartPhone කියන class එකින් හදපු object එකේ call method එකට කතා කලාම ලැබෙන output එක.

class CreatingObjects{
      public static void main(String a[]){

             SmartPhone smartphone1=new SmartPhone();
             smartphone1.call();
      }
}

Phone class එකේ call method එකට කතා කලාම ලැබෙන output එකනම් හරි නේද..SmartPhone class එකේ call method එකට කතා කලාම ලැබෙන output එක නම් වැරදියි නේද..SmartPhone class එකේ call method එකට කතා කලාම මොකක් හරි output එකක් ලැබුනනෙ ..ඒ කියන්නෙ අපි inheritance කරපු එකේ ප්‍රශ්නයක් නෑ.call method එක SmartPhone Class එක ඇතුලට ඇවිත් තියෙන්නේ.නමුත් ඇවිත් තියෙන්නෙ Phone කියන class එකේ තිබුන call method එකේ body එකත් ඇරගෙන නේද..ඒ නිසා තමයි අපිට SmartPhone class එකේ තියන call method එකට කතා කලාම Phone class එකේ තිබුන call method එකේ body එක execute වෙලා පෙන්නුවේ .එහෙනම් අපිට SmartPhone class එකට අවශය විදියට call method එක execute වෙන්න ඕනි නම් ,SmartPhone class එකේ call method එකේ body එක මෙන්න මේ විදියට වෙනස් කලානම් හරි නේද..

class Phone{
      String color;
      int price;

      void call(){
           System.out.println("This phone can  take audio calls only ! ");
      }
}

class SmartPhone extends Phone{
     String storage;

     void takePhoto(){
           System.out.println("This phone can take quality photos ! ");
     } 
 
  
   void call(){
           System.out.println("This phone can  take audio calls and video calls ! ");
      }
   
}

class CreatingObjects{
      public static void main(String a[]){

             SmartPhone smartphone1=new SmartPhone();
             smartphone1.call();
      }
}

දැන් බලන්නකෝ output එක දිහා. දැන්නම් හරි නේද..

ඔන්න ඕකට තමයි යාලුවනේ method overriding කියලා කියන්නෙ.කෙටියෙන්ම කිව්වොත් එක class එකකින් තවත් class එකක් inherit කලාම super class එකේ තියන methods , sub class එකට අවශය ආකාරයට ඒවායේ body එක වෙනස් කරලා පාවිච්චි කරන සිද්ධියට තමයි method overriding කියලා කියන්නේ.ඔන්න ඔයාලට මං ආයෙත් කියනවා method overloading එක්ක මේක පටලව ගන්නනම් එපා.එකම නමින් methods ගොඩාක් ඒවයේ parameter signature එක වෙනස් කරලා එකම class එකක් ඇතුලේ define කරන එකට තමයි අපි කිව්වෙ method overloading කියලා.එකම method එකක body එක තමන්ට ඕනි විදියට වෙනස් කරලා පාවිච්චි කරන එකට තමයි අපි method overriding කියලා කිව්වේ.ඔන්න ඕකම තමයි මේ දෙක අතර තියන වෙනස.හරි තේරුනා නේද යාලුවනේ method overriding කියන්නෙ මොකක්ද කියලා.

හරි එහෙනන් අපි බලමු method overriding වලදි හමුවන keyword එකක් ගැන.ඒ තමයි super keyword එක.ඇත්තටම යාලුවනේ super keyword එකින් කරන්නෙ යට තියන දෙයක් උඩට ඇදලා දෙන එක.😃😃😃 තේරුන් නෑ නේද කිව්ව දේ..හරි ඔයාලා දන්නවනේ inherit වෙලා තියන class එකක super class එකේ method එකක body එක වෙනස් කරලා sub class එකේදි ඒ method එක පාවිච්චි කරන විදිය ගැන (method overriding).දැන් ඔයා හිතන්න ඒ විදියට sub class එකේ තියන method එකක body එක වෙනස් කරගෙන යද්දී ඔයාට ඕනි වෙනවා super class එකේ තියන ඊට අදාළ method එකේ body එක..එහෙම කරන්ටනං ඔයා අර sub class එකේ override කරපු method එක ඇතුලෙ ඉදලා super class එකේ ඒ නමම තියන method එක call කරන්න ඔනි නේද..තේරුනා මදි වගේනම් කලින් උදාරණයම ගමුකෝ.

මට ඕනි දැන් sub class එකේ call method එකේ තියන පලමු statement එක output එක විදියට දීලා ඊලග statement එකේදී super class එකේ call method එකට කතා කරන්න.ඒ කියන්නෙ මෙන්න මේ විදියට.

class Phone{
      String color;
      int price;

      void call(){
           System.out.println("This phone can  take audio calls only ! ");
      }
}

class SmartPhone extends Phone{
     String storage;

     void takePhoto(){
           System.out.println("This phone can take quality photos ! ");
     } 
 
  
   void call(){
           System.out.println("This phone can  take audio calls and video calls ! ");
           call();

      }
   
}

class CreatingObjects{
      public static void main(String a[]){

             SmartPhone smartphone1=new SmartPhone();
             smartphone1.call();
      }
}

output එකත් බලන්නකෝ.

error එකක් දෙනවා නේද interpreter එකෙන්.ඔන්න ඔය එන error එක වලක්වා ගන්න තමයි අපි super keyword එක පාවිච්චි කරන්නේ.

class Phone{
      String color;
      int price;

      void call(){
           System.out.println("This phone can  take audio calls only ! ");
      }
}

class SmartPhone extends Phone{
     String storage;

     void takePhoto(){
           System.out.println("This phone can take quality photos ! ");
     } 
 
  
   void call(){
           System.out.println("This phone can  take audio calls and video calls ! ");
          super.call();

      }
   
}

class CreatingObjects{
      public static void main(String a[]){

             SmartPhone smartphone1=new SmartPhone();
             smartphone1.call();
      }
}

output:

දැන් හරි නේද යාලුවනේ.ඔන්න ඕක තමයි super keyword එකේ කාර්යය.ඇත්තටම කියනවනම් ඔය super keyword එක කරන්නෙ මම අර කලින් කිව්වා වගේ යට තියන දෙයක් උඩට ඇදීමක්. super class එකේ තියන method එකක් sub class එකේදී override කලාම වෙන්නෙ sub class එකේ override කරපු method එකෙන් super class එකේ තිබුන ඒ method එක වැහිලා යන එකයි.super keyword එකින් කරන්නෙ ඒ වැහිලා ගියපු method එක අයෙත් උඩට ඇදලා පෙන්නන එකයි.අන්න ඒකයි මං කිව්වෙ super keyword එකෙන් කරන්නෙ යට තියන දෙයක් උඩට අදින එක කියලා.

හරි යාලුවනේ තේරුනා නේද method overriding ගැනයි super keyword එකේ භාවිතය ගැනයි.මොකක් හරි අව්ලක් ආවොත් කොමෙන්ටුවක් දාන්න ඕනි හොදේ ☺☺☺.අදට පාඩම අවසන්.object oriented concept එකේ තියන තවත් feature එකක් සමගින් මීලග පාඩමෙන් හමුවෙමු.සුභ දවසක්.ස්තූතියි!