Reading Time: 4 minutes

ඔන්න යාලුවේ අදත් මං ආවා කලින් පාඩමේ ඉතිරි ටිකත් සමගින්.කලින් පාඩමේදි අපි කථා කලා method overriding සහ constructor එක ගැන. මොකද්ද අපි method overriding කියල කිව්වෙ? method overriding. කියල අපි කිව්වෙ එකම නමින් තියෙන method එකක body එක අපිට ඕනි විදියට වෙනස් කිරීමයි.method overloading හා method overriding අතර වෙනසත් අපි කතා කලා.කලින් පාඩම අමතකනම් හරි බැලුවෙ නැත්නම් හරි බැලුවොත් හොදයි කියල හිතනවා. method overriding වලට පසුව අපි කතා කලා constructor එක ගැන.මොකද්ද අපි constructor එක කියල කිව්වේ?, constructor එක කියල අපි කිව්වෙ, class එකේ නමින්ම තියෙන object එකක් හදන වෙලාවෙදි පමණක් call වෙන return type එකක් නැති method එකකට.හරි දැන් constructor එක ගැනත් මතක් වෙන්න ඇති කියල හිතනවා.

හරි අද පාඩමේදි අපි කතා කරන්න යන්නේ constructor එක හරහා values pass කරන්නේ කොහොමද කියන එක ගැන.construcor එක කියන්නේ method එකක්මයි කියල මං කලින් පාඩමේදී කිව්වනෙ.එහෙනම් method එකක values pass කරන විදියටම අපිට පුලුවන් constructor එක හරහාත් values pass කරන්න.හැබැයි ඊට කලින් attributes හා ඒවාට අදාල parameters ටික constructor එකේ වරහන් ඇතුලෙ define කරගෙන ඉන්න ඕනි කලින්ම.පසුව constructor එකේ body එක ඇතුලෙ attributes වලට ඔයා ගත්ත parameters ටික assign කර ගන්නත් ඕනි.ඔන්න ඔය ටික තමයි constructor එකක් හරහා values pass කිර්‍ර්මේදි ඉස්සරෝම කරන්න ඕනී.method එකක නම් object එකක් හදාගෙන dot operator එක පාවිච්චි කරල method එක call කරලානෙ values pass කරන්නෙ.constructor එකේ අපි values pass කරන්නෙ object එක හදන වෙලාවෙමයි.ටිකක් තේරුනා මදි වගේ නේද? එහෙනම් මෙන්න මේ උදාහරණය බලන්නකෝ.

මම හදනව class එකක් car කියල.brand කියලා attribute එකකුත් හදා ගන්නවා class එක ඇතුලෙම.එතකොට car class එකේ constructor එක වෙන්නේ car() කියලනේ.හරි දැන් මට ඕනි car1 කරලා object එකක් හදලා car1 වල brand එක constructor එක හරහා value එකක් විදියට pass කරලා object එක හදනකොට car1 වල brand එක print කරන්න.මෙන්න මෙහෙම කලා නම් හරි නේද බලන්නකෝ.

class Car{

    Car(String brand){
          System.out.print("My Car brand is "+brand);
    }

}

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

          Car car1 = new Car("Toyota");
    }
}

Output :

හරි දැන් ඔයාලට පේනවා ඇති car1 කියන object එක හැදුවම brand එක print වෙලා තියනවා කියන එක.ඒ කියන්නෙ constructor එක හරහා value එකක් pass වෙලා තියෙන බව.ඔන්න ඔහොම තමයි constructor එක හරහා values pass කරන්නේ.හැමෝටම තේරුනා කියල හිතනවා.
හරි අපි කලින් කිව්වනෙ constructor එක කියන්නේ method එකක් කියල.දැන් ඔයාලට ප්‍රශ්නයක් ඇති constructor එක overload කරන්න පුලුවන්ද කියලා.කෙටියෙන්ම කිව්වොත් පුලුවන්.ඇයියේ කියනවනම් constructor එක කියන්නෙත් method එකක් නිසා. අනෙක් method overloading කරන විදියටම තමයි constructor එකත් overloading කරන්නේ.එකම විශේෂත්වය තමයි method overloading එකේදි අපි කරන්නෙ object එක හදලා අදාල method එක call කරන එකනෙ.constructor එකක් overload කිරීමේදි අපි කරන්නේ object එක හදනකොටම values දීල.අපි ලබාදෙන values ගනනෙන් හරි ඒවායේ data type එකේ වෙනසින් හරි තමයි compiler එක හදුනා ගන්නේ කොයි constructor එකද මේ call කරන්නෙ කියල.ඔන්න ඔය constructor එක overload කරන එකට අපි කියනවා constructor overloading කියලා.තේරුනා මදි වගේනම් මෙන්න මේ උදාහරණයත් බලන්නකෝ.මම මගෙ කලින් පාඩමේ හඩතල වල වර්ගපලය හොයන උදාහරනයම ගන්නම්.

class Area{
   double c;
   int A;

   Area(double x){
         c=14*x*x;
         System.out.print("aria of circle is :"+c);}

  Area(int x){
        A=3.14*x*x;
        System.out.print("aria of square is:"+A);}
  
  Area(int x,int y){
        A=x*y;
        System.out.print("aria of rectangle is :"+A);}
  }


class CalArea{
      public static void main(String a[]){
      
              // if you want to calculate the area of circle,give a double value  in creating an object
                      Area A1=new Area(5.5);
       
              // if you want to calculate the area of square,give an integer value  in creating an object
                      Area A2=new Area(5);

              // if you want to calculate the area of rectangle,give two integer values  in creating an object
                     Area A3=new Area(5,10);
           
  
      }

}

Output :

හරි දැන් තේරුනා නේද constructor overloading කියන්නෙ මොකක්ද කියලා.හරි අපි දැන් බලන්න යන්නේ object oriented concept එකේ තියෙන වැදගත් keyword එකක් ගැන.ඒ තමයි ‘this’ කියන keyword එක.this keyword එකෙන් කරන්නෙ method එකෙන් එලියෙ තියෙන attribute එකකට call කරන එක. this keyword එකේ කාර්යය මොකක්ද කියලා මෙන්න මේ උදාහරණයෙන් තවදුරටත් පැහැදිලි කර ගනිමු.කලින් උදාහරණයම මං ගන්නවා.constructor එකේ parameter එක විදියට මම attribute එකේ නමම තියෙන parameter එකක් ගන්නවා .ඒ කියන්නෙ parameter එකේ නම විදියට brand කියන එකම ගන්නවා.දැන් constructor එක call කලොත් ඔයාලට පේනව ඇති null කියලා තමයි print වෙලා තියෙන්නෙ.(මට අවශ්‍ය වෙන්නේ attribute එකේ value එක නිසා මම print කරනවා this.brand කියලා.)

ඒ කියන්නෙ අපි දුන්නු value එක හරි හැටි attribute එකට pass වෙලා නැහැ.මෙන්න මේ ගැටලුව විසදන්න අපිට මේ this කියන keyword එක පාවිච්චි කරන්න පුලුවන්.ඔන්න බලන්නකෝ this keyword එක පාවිච්චි කලාම output එක.

class Car{
	
	String brand;

	Car(String brand){
	      this.brand = brand;
	      System.out.print("My car brand is :"+this.brand);
	}

}

class MyCar{
	public static void main(String a[]){
		
		Car car1=new Car("toyota");
	}


}

Output :

දැන් value එක හරි හැටි pass වෙලා තියනවා කියලා ඔයාලට තේරෙනව ඇති.this keyword එකෙන් වෙන්නෙ ඔන්න ඔය කාර්යය තමයි.ඒ කියන්නෙ method එකක තියෙන parameter එකක් හෝ කිහිපයක් class එකේ define කරලා තියෙන attibutes වල නමින්ම තියනවනම් ,ඒ ඒ attribute වලට ඊට අදාල parameter එකේ value එක assign කිරීමට මේ this කියන keyword එක පාවිච්චි කරන්න පුලුවන්.හරි දැන් තේරුනා නේද this keyword එක පාවිච්චි කරන්නෙ ඇයි කියලා.අද පාඩම මෙතනින් නිමයි.මීලග පාඩම තුලින් හමුවෙමු.ස්තූතියි!

Categories: OOP

Gayan Sampath

Undergraduate at University of colombo school of computing | Blogger @thesigma.gq

Leave a Reply

Your email address will not be published. Required fields are marked *