Sunday, February 9, 2014

OOP තෙවැනි පාඩම




මං අද ඔයාලට කියල දෙන්න හදන්නේ oop වල තියන තවත් වටිනාම සිධාන්තයක් වන inheritance ගැන තමයි.inheritance ගැන ඉන්ටෙර්නෙට් එකේ විකිපීඩියාවේ දැක්වෙන්නේ මෙන්න මෙහෙමයි. http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 මේ ලින්ක් එකට ගහින් බලන්න පලුවන්.කෙටියෙන්ම කිවොත් inheritance කියන්නේ අපි කලින් හදාගත්තු class එකක් අපි හදන වෙනත් class එකකට refer කරන්න පලුවන් වීම තමයි.කරගෙන යනකොට පෙනේවි මොකක්ද වෙන්නේ කියල.

ජාවා වල class හා interfaces කියල වර්ග දෙකක් තියනවා.classes inherit කරන්න extend කියන key word එකයි interface inherit කරන්න implements කියන key word එකයි use කරනවා.

දැන් අපි පොඩි උදහරනයෙක් අරං බලමු.
/**
 *
 * @author KASUN002
 */
class A {

    int a=5;

    void printA() {
        System.out.println("Class A");
    }
}

class B extends A {

    int b=10;

    void printB() {
        System.out.println("B : " + b);
    }

    void printAB() {
        System.out.println("a : b " + a + " " + b);
    }

}

public class Demo {
    public static void main(String args[]){
        A a_new;
        a_new = new A();//making a object using A class
        a_new.printA();
       
        B b_new=new B();//making a object using B class
        b_new.printA();
        b_new.printB();
        b_new.printAB();
    }
}
 
මේ උදාහරණයේ class A use කරලා a_new කියල object එකක් හදල තියනවා.එකට පලුවන් printA කියන method එක refer කරන්න විතරයි.නමුත් බලන්න classB හදල තියන්නේ classA extends කරලා ඒ කියන්නේ classA වල තියන සියලුම ලක්ෂණ classB වලට එනවා.එක නිසා තමයි classb වලින් හදපු object එකට classA වල තියන veriables හා methodes refer කරන්න පලුවන් වෙලා තියෙන්නේ.සරලවම extends කරවිට වෙන්නේ ඕක තමා.දැන් අපි බලමු static block එකක් class වල තිබාම වෙන්නේ මොකක්ද කියල.

class A{
      int a;
      static{
            System.out.println("Class A");//Static block
      }
      void printA(){
            System.out.println("A : "+a);
      }
}
class B extends A{
      int b;
      static{
            System.out.println("Class B");//Static block
      }    
      void printB(){
            System.out.println("B : "+b);
      }
      void printAB(){
            System.out.println("a : b "+a+" "+b);
      }
      void myMethod(){
            printA();
            printB();
      }
}
class Demo{
      public static void main(String args[]) {
            B b1=new B();
      }
}
අපි මේ උදාහරණය සැලකුවොත් classA වලින් තමයි classB extends වෙලා තියෙන්නේ එහෙනම් classA වලට කියනවා Super class කියල classB වලට කියනවා Sub class කියල.දැන් මේ program එක run කරලා බලුවොත් පේනවා class A ,class B කියල ප්‍රින්ට් වෙනවා.නමුත් අපි හදල තියෙන්නේ B වලින් object එකක් ව්තරයි.නමුත් method call කරලා නෑ.නමුත් class A ,class B කියල ප්‍රින්ට් වෙනවා.මේක වෙන්නේ class එකකින් object එකක් හදනකොටම class එකේ තියන static block run වෙනවා.මේ classB classA ගෙන් extends වෙලානේ.එක නිසා මුලින්ම run වෙන්නේ  Super class එකේ static block එක ඊට පස්සේ sub class එකේ static block එක.

දැන් අපි ඊළඟට බලමු inheret වෙච්ච class එකකින් object එකක් හැදුවම මුලින්ම run වෙන්නේ මොන constructor එක ද කියල.මේ program එක බලන්න...

 class A{

                int a;

                A(){

                                System.out.println("A()");

                }

                A(int i){

                                System.out.println("A(int)");

                }    

}

class B extends A{

                B(){

                                System.out.println("B()");

                }          

}

class Demo{

                public static void main(String args[]) {

                                new B();

                }

}
Demo class එකේ දි call කරන්නේ B class එකේ constructor එක නමුත් output එක මේ වගේ තමයි ලැබෙන්නේ.

C:\Users\KASUN002\Desktop>java Demo

A()

B()

මුලින්ම call වෙලා තියෙන්නේ class A වල constructor එක එහෙනම් මුලින්ම run වෙන්නේ super class එකේ constructor එක.ඊටපස්සේ අනුපිලිවලට sub class වල constructors run වෙනවා.තවදුරටත් කිවොත් අපි හිතමු class B වලට තියනවා කියල තව constructor එකක් අපි Demo class එක ඈතුලේ ඒ තියන අනිත් constructor එක call කොරොත් මුලින්ම class A වල default constructor call වෙලා තමයි class B වල pharameeters තියන constructor එක call වෙන්නේ.මේ programme එක වගේ..

class B extends A{

      B(){

            System.out.println("B()");

      }    

      B(int i){

            System.out.println("B(int)");

      }                

}

class Demo{

      public static void main(String args[]) {

            new B(1000);

      }

}

ඔයාලට පේනවා ඇති මේ විදියට programme එක තිබුනොත් හැම නිතරම super class එකේ default constructor එක විතරයි නේද.එතොකොට අපිට අවශ super class constructor එක run කරගන්න නම් super කියන key word එක use කරන්න ඕන වෙනවා.මේ බලන්න මේ programme එකයි එකේ output එකයි..

class A{

      int a;

      A(){

            System.out.println("A()");

      }

      A(int i){

            System.out.println("A(int)");

      }

      A(int i,int j){

            System.out.println("A(int,int)");

      }          

}

class B extends A{

      B(){

            super();

            System.out.println("B()");

      }    

      B(int i,int j){

            super(100);

            System.out.println("B(int,int)");

      }                      

}

class Demo{

      public static void main(String args[]) {

            new B();

            new B(100,200);

      }

}
Output එක මේ වගේ..

C:\Users\KASUN002\Desktop>java Demo

A()

B()

A(int)

B(int,int)

දැන් තේරෙන්න ඇති අපිට අවශ super class constructor එක run කරගන්නේ කොහොමද කියල.

දැන් මේ program එක ටික්කක් බලන්න. මේකේ කලින් programme වලට වඩා පොඩි වෙනසක් තියනවා.මොකද කිවොත් super class එකේ constructor එක call කරනකොටම this කියල යොදල තියනවා .super & this එකට යොදන්න බැ compile error එකක් එනවා .

class A{

                int a;

                A(){

                                System.out.println("A()");

                }

                A(int i){

                                System.out.println("A(int)");

                }    

                A(int i,int j){

                                System.out.println("A(int,int)");

                }          

}

class B extends A{

                int b;

                B(){

                                super();

                                System.out.println("B()");

                }    

                B(int i){

                                this(i,i);

                                super(i);

                                System.out.println("B(int)");

                }          

                B(int i, int j){

                                super(i,j);

                                System.out.println("B(int,int)");

                }                

}

class Demo{

                public static void main(String args[]) {

                                new B(100);

                }

}

අපි දැන් බලමු super class object එකක් sub class එකක් reference කරන්නේ කොහොමද කියල.මුලින්ම අපි programme එක බලමු.

class A{

                int a;

                void printA(){

                                System.out.println("A : "+a);

                }

}

class B extends A{

                int b;

                void printB(){

                                System.out.println("B : "+b);

                }

                void printAB(){

                                System.out.println("a : b "+a+" "+b);

                }

}

class Demo{

                public static void main(String args[]) {

                                A a1=new B();

                                a1.a=100;

                                //a1.b=200;//Error

                                a1.printA();

                                //a1.printB();//Error

                }

}

මේකේ බලන්න a1 කියල object එකක් හදනවා A class එකින් class B reference කරගෙන (A a1=new B();).එහෙම use කරන්න පලුවන් අව්ලක් නැතුව.දැන් a1 object එකට පලුවන් use කරන්න A class එකේ properites.නමුත් B class එකේ properties use කරන්න බෑ.හේතුව තමයි super class object එකක් sub class එකක් reference කරාම ඊ object එකට තියෙන්නේ super class එකේ properties විතරයි.නමුත් sub class object එකකට super class එකක් reference කරන්න බැ.එහෙම කරන්න හැදුවොත් compile error එකක් එනවා.

දැන් මේ programme එක බලමු ..

class A{

                int a;

                void printA(){

                                System.out.println("A : "+a);

                }

                void myMethod(){

                                System.out.println("A.myMethod()");

                }    

}

class B extends A{

                int b;

                void printB(){

                                System.out.println("B : "+b);

                }

                void myMethod(){

                                System.out.println("B.myMethod()");

                }          

}

class Demo{

                public static void main(String args[]) {

                                A a1=new B();

                                a1.myMethod();

                }

}
Output of this program

C:\Users\KASUN002\Desktop>javac Demo.java



C:\Users\KASUN002\Desktop>java Demo

B.myMethod() 

මේ class එකේ main method එකේ හදපු a1 කියන object එක අපි කලින් කතාකරා වගේ super class object එකක් sub class එකක් reference කරපු අවස්තාවක් තමයි.නමුත් run කරාම ඇවිත් තියන output එකේ දි run වෙලා තියෙන්නේ sub class එකේ තියන method එකක්.නමුත් කලින් උදාහරණයේදී sub class එකේ properties use කරාම compile error එනවා කියල කිවා.නමුත් මෙතැනදී එන්නේ නෑ නේ.එක වෙන්නේ මෙහෙමයි.හොඳට බලන්න මෙතන run වෙන method එකේ නමින්ම තව method එකක් super class එකේ තියනවා ඒ method දෙකේ නම්  දෙක සමාන වූනත් method body දෙක සමාන නෑ.මේ වගේ අවස්ථාවකට කියෙන්නේ method overriding කියල.method එකක්  override වූනාම run වෙන්නේ sub class එකේ override වෙච්ච  method එක.එක නිසා තමයි ඊ වගේ output එකක් කලින් programme එකේ දි ආවේ.

දැන් අපි ටිකක් බලමු super කියන key word එක use කරන විදිය ගැන..


class A{

                int a=100;

}

class B extends A{

                int a=200;

               

                void myMethod(){

                                int a=300;

                                System.out.println("Local a : "+a);

                                System.out.println("Sub   a :"+this.a);

                                System.out.println("super a : "+super.a);

                }

               

}

class Demo{

                public static void main(String args[]) {

                                B b1=new B();

                                b1.myMethod();

                }

}
Output of this program


C:\Users\KASUN002\Desktop>java Demo

Local a : 300

Sub   a :200

super a : 100

ඔයාලට පේනවා ඇති අපිට sub class එක ඇතුලේ ඊට අදාල super class එකේ veriable එකක් හරි method එකක් හරි refer  කරන්න ඕනෙනම් super. කියල අවශ veriable එක හරි method එක හරි call කරන්න ඕනේ කියල.this කියල කිවොත් දැන් ඉන්න class එකමයි refer කරන්නේ.මේ ගැන කලින් පාඩමකදීත් සගච්චා කරා මතක ඇතිනේ.

අන්තිමටම අපි දැන් බලමු object class එක ගැන . හිතන්න අපි class එකක් හදනවා නිකම්ම class A කියල.නමුත් මේ class එක extends වෙලා තියෙන්නේ ජාවා වල තියන object කියන class එකින්.ඒ කියන්නේ අපි හදන ඕනෑම class එකක් මුලින්ම extends වෙන්නේ object class එකින්.එක compiler automatically insert කරනවා.එක වෙනවද කියල පහත තියන programme එකින් හරියටම බලාගන්න පලුවන්.

class A {//"extends Object"



}

class Demo{

      public static void main(String args[]) {

            A a1=new A();

            A a2=new A();

            System.out.println(a1.hashCode());

            System.out.println(a2.hashCode());

            System.out.println(a1.toString());

      }

}

මෙතන hashCode() කියල call කරලා තියන method එක object class එකේ තියෙන්නේ.ඒගැන වැඩි විස්තර ඕනනම් API එකට ගහින් බලාගන්න පලුවන්.මේ ප්‍රෝග්‍රෑම් එකේ a1 ,a2 ට එන hashCode() අගයන් දෙක වෙනස්. එතකොට මේකින් පැහැදිලිවෙනවා සෑම class එකක්ම compiler විසින් object class එකින් extends කරනවා කියල.

මං හිතන්නේ inheritance ගැන පොඩි හරි වැටහීමක් ඔයගොල්ලොන්ට මේ පාඩමෙන් ලැබෙන්න ඇති කියල.නැවත අලුත් පාඩමකින් හමුවෙමු.....

No comments: