Monday, January 27, 2014

OOP දෙවන පාඩම

Constructor Overloading & Method Overloading  
 OOP දෙවැනි පාඩමෙන් මුලින්ම මං කතාකරන්න බලාපොරොතු වෙන්නේ overloading ගැන.overloading ගැන කතාකරනකොට කොටස් දෙකක් ගැන කතාකරන්න වෙනවා.ඒවා තමයි
         1)constructor overloading
           2) method overloading
අපි මුලින්ම කතාකරමු constructor overloading ගැන.කෙටියෙන්ම කිවොත් constructor overloading කියන්නේ class එකකට constructors ගොඩක් තියෙන්න පුළුවන්.අපි කලින් පාඩමේදී කතාකලා constructor එකක් කියන්නේ class name එකින්ම තියන return type එකක් නැති method එකක් කියල.තව කිව්ව class එකක් හදනකොටම ජාවා වලින් default constructor එකක් automatically class එකකට add කරනවා කියල.නමුත් දැන් ඔයාලට හිතෙන්න ඕනේ එහෙනම් කොහොමද එක class එකකට constructors ගොඩක් යොදන්නේ කියල.එක වෙන්නේ මෙහෙමයි.අපි යොමුවෙමු කලින්ම කතාකරපු Box class එකට.
අපිට මේවර ඕනේ කරන Box එක කලින් එකට ටිකක් වෙනස්.Box එකක හදනකොට එක අවස්ථාවක length,higth ,width තුනම 0 වෙලා තියන්න ඕනේ,නමුත් තවත් වෙලාවක Box එකේ length,higth ,width තුනම object එක හදනකොට pass කරන අගයන් වලට අනුකුලව වෙනස්වෙන්න ඕනේ.එකට class එක ලියන විදිය අපි දැන් බලමු.
class Box{
      int length;
      int width;
      int height;
     
      Box(){// Default constructor method
            length=0;
            width=0;
            height=0;
            System.out.println("Box....**** Constructor ****");  
      }
     
      Box(int length,int width,int height){//overloaded constructor
            this.length=length;
            this.width=width;
            this.height=height;
            System.out.print("User inserted constructor");
      }
}
Main class
class demoBox {
      public static void main(String args[]){
            Box b=new Box();
            Box b1=new Box(10,10,10);
      }
}

මේ programme එක run කරලා බලුවම ඔයාලට පනේවි b කියල object එක හදනකොට run වෙන්නේ parameters නැති constructor එක,b1 කියල object එක හදනකොට run වෙන්නේ parameters 3 තියන constructor එක.ඒ අනුව පේනවා ඇති එකනමින් තියන constructors කිහිපයක් වූවත් class එකක include කරන්න පලුවන්.නමුත් ඒ include කරන constructors වල තියන parameters වෙනස් වෙන්න ඕනේ.දැන් පහත තියන constructors දෙක බලන්න.

Box(int length,int width,int height){//overloaded constructor 1
            this.length=length;
            this.width=width;
            this.height=height;
            System.out.print("User inserted constructor");
      }
Box(int length,int width,float height){//overloaded constructor 2
            this.length=length;
            this.width=width;
            this.height=height;
            System.out.print("User inserted constructor");
      }

මේ දෙකම එක class එකක් ඇතුලේ දැම්මොත් class එක compile වෙනවද?parameters 3 බැලුබැල්මට කියන්නේ නෑ කියල නමුත් අව්ලක් නැතුව compile & run වෙනවා.මොකක්ද දන්නවද හේතුව දෙවනි constructor එකේ අන්තිම parameter එක float එක නිසා parameters සර්වසම නෑ.මෙතනදීත් constructor overloading වෙනවා කිසි අව්ලක් නැතිව.අපි තව පොඩි උදාහරණයක් බලමු.

binaryTree(int left,double right){
            this.left=left;
            this.right=right;
      }

binaryTree(double right, int left){
            this.left=left;
            this.right=right;
      }

ඉහත දක්වල තියන constructors දෙක parameters ගාන වගේම data type ත් සමානයි නමුත් compile error නැතිව run වෙනවා.හේතුව තමයි parameter දෙකේ order එක වෙනස්.මේවගේ අවස්ථාවකදීත් වෙන්නේ constructor overloading තමයි.දැන් අපි පොඩි අලුත් වැඩක් කරමු constructor overloading වලට.අපිට constructor overloading බලාගන්න හොඳම විදිය තමයි ජාවා API එක.
API කීවට හරියටම තේරෙන්නේ නැතිව ඇති.ජාවා වල API එක කියන්නේ jdk එකත් එක්ක එන documentation එක .එක බලන විදිය පොඩ්ඩක් කියලම ඉන්නම්.මුලින්නම කරන්න ඕනේ API එක download කරගන්න එක හරි online API page එකට යනඑක තමයි.ඒ දෙකට ලින්ක් දෙක පහත දක්වල තියනවා.

http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html මේ ලින්ක් එකට ගහින් API එක download කරගන්න.download වෙන්නේ rar file එකකක්.එක extract කරලා එන file එකක් වෙබ් බ්‍රොව්සෙර්ට book mark එකක් තියාගන්න.

නැත්තම් http://docs.oracle.com/javase/7/docs/api/ මේ ලින්ක් එකට ගියාම online API එක බලන්න පලුවන්.
මේ API එකේ තියෙන්නේ ජාවා class ගැන summary එකක්.පහත දැක්වෙන්නේ ජාවා API එකේ Screen Shot එකක්.




දැන් නැවත constructure overloading වලට හැරෙමු.දැන් අපි ජාවා class එකක් API එකින් බලමු.



All classes වල JButton class එක හෝ ඔයාලට කැමති ඕනෑම class එකක් click කරන්න.ඊතලයෙන් පෙන්නලා තියන විදියට.එතකොට class එකේ විස්තර යතේ එන constructor summary යතේ එන්නේ එම class එකට තියන constructor ටික තමයි.ඔයාලට පේනවා ඇති මේකේ same name different signature තියන constructors ගොඩක් තියනවා.එතන කරලත් තියෙන්නේ constructor overloading.ඒ constructors ගැන වැඩිදුර තොරුතුරු ඕනනම් constructor එක උඩ click කරන්න.එතකොට විස්තර පෙන්නාවි.දැන් ඔයාලටම පලුවන් API එක use කරලා constructor overloading කරන්නේ කියල තනියම කරන්න.
දැන් අපි method overloading කරන විදිය ගැන ටිකක් බලමු.හරියටම කිවොත් constructor overloading නුයි method overloading නුයි අතර එචර වෙනසක් නෑ එකවගේම තමයි.මුලින්නම අපි method overloading වලට පොඩි උදාහරණයක් බලමු.

class Calculator {
      int sum;
      double sum1;

   public void add(int i, int j) {
         sum=i+j;
        System.out.println("Integer addition"); 
   } 
    
   public void add(int i, double j) { 
         sum1=i+j;
        System.out.println("Integer and double addition"); 
   } 
  
   public void add(double i, double j) { 
         sum1=i+j;
        System.out.println("Double addition"); 
   } 
  
මේ calculator class එකේ add method 3 තියනවා එකම parameters ගාන තියන.නමුත් parameters වල order එක හා data type වල එකිනෙකට වෙනස්කම් තියනවා.ඒ කියන්නේ එකම නමින් method ගොඩක් එකම class එකේ include කරලා තියනේ.නමුත් different signature වලින් මේ ක්‍රියාවලිය තමයි method overloading කියන්නේ.දැන් ඔයාලට තේරෙනවා ඇති constructor overloading වලදී වගේම තමයි method overloading වලදී වෙන්නෙත් කියල.පහත දක්කවා ඇති සම්පුර්ණ Box class එක බලන්න.
class Box{
      int length;
      int width;
      int height;
     
      Box(){
            length=1;
            width=1;
            height=1;
      }
     
      Box(int l, int w, int h){
            length=l;
            width=w;
            height=h;
      }

      void printVolume(){
            System.out.println("Volume is:"+getVolume());
      }
     
      void setValues(int l, int w, int h){
            length=l;
            width=w;
            height=h ;
      }
     
      void setLength(int l){
            length=l;
      }
     
      void setWidth(int w){
            width=w;
      }
     
      void setHeight(int h){
            height=h;  
      }
     
      int getVolume(){
            int volume;
            volume=length*width*height;
            return volume;
      }
     
      void printArea(){
            int a=getArea();
            System.out.println("Area of the Box is :"+a);
      }
     
      int getArea(){
            int a;
            a=2*(length*width+length*height+width*height);
            return a;
      }
     
      int getLength(){
            return length;
      }
     
      int getWidth(){
            return width;
      }
     
      int getHeight(){
            return height;
      }
     
      void printValues(){
            System.out.println("Length :"+length);
            System.out.println("Width  :"+width);
            System.out.println("Height :"+height);
      }
     
      void setValues(int l){
            length=l;
            width=l;
            height=l;
      }
}
ඒ Box class එකේ constructors මෙන්නම method overloading වෙලාතියනව.set values කියන method එක මුල් එකට parameters 3 යි අනිත් එකට parameters 1 යි.overload කරපු විදිය තේරෙනවා ඇතිනේ.දැන් ඔයාලට පලුවන් API class එකක් අරන් method overloading use කරලා තියනවිදිය බලාගන්න.

මං අන්තිමටම් කියන්න යන්නේ API එක download කරලා browser එකට bookmark එකක් දාගන්න විදිය.

1)මුලින්ම download page එකට ගිහින් liaison agreement එකට agree වෙන්න.
2)ඊ හිසෙන් දක්වා ඇති file එක මත click කරවිට download වීම ඇරඹේ.
3)download වූ file එක extract කරගන්න.
  
4)එතොකොට docs කියල folder එකක් ලැබෙනවා .
5)ඒ folder එක ඇතුලේ මේ වගේ file වගයක් තියනවා.
6)එකේ තියන API folder එකට යන්න.එකේ ඇති index කියල file එකක්,එක වෙබ් browser එකින් open කරන්න.
7)ඊටපස්සේ මේ page එක ලැබෙනවා.
 

8) දෙවෙනි රූපෙ තියන තරු සලකුණ click කරලා book mark එක දාගන්න.

මන් හිතන්නේ overloading ගැන වගේම API එක ගැනත් පූංචි දැනුමක් මේ පොස්ට් එකින් ලැබෙන්න ඇති කියල.අපි ඊළඟ පාඩමේදී inheritance ගැන කතාකරමු............. 

  

No comments: