Reading Time: 4 minutes

ආයුබෝවන් ! oop පාඩම් මාලාවේ 11 වන දිගහැරුමත් සමග එකතු වෙන යාලුවෝ හැමෝම සාදරයෙන් පිලිගන්නවා…the sigma blog අඩවියත් සමග නිරන්තරයෙන් රැදී සිටි යාලුවෝ නම් මේ වෙනකොටත් object orianted programming ගැන ගොඩක් දේවල් ඉගෙනගෙන හමාරයි.ඔයා the sigma blog අඩවියට අලුත් සාමාජිකයෙක් නම් හරි , oop අව්ල් කෙනෙක් නම් හරි අපේ මුල් පාඩම් ටික මෙතනින් ගිහින් බලන්න අමතක කරන්නත් එපා හොදේ. හරි එහෙනන් පටාන් ගමු අද පාඩම…අද පාඩම තමයි abstract class …. abstract class ගැන කතා කරන්න ඉස්සරින් කලින් පාඩම පොඩ්ඩක් මතක් කරන්නම්…කලින් පාඩමේදී අපි කතා කලා. encapsulation ගැන…මොකද්ද අපි encapsulation කියලා කිව්වේ… class එකක තියන data protect කරන එක එහෙමත් නැත්නම් data ආරක්ෂා කරන එකට අපි කිව්වා encapsulation කියලා…. අපේ encapsulation පාඩම මතක නැතිනම් මෙතනින් ගිහින් ඉක්මනට බලනකෝ…එතකොට සුපිරියටම මතක් වෙයි.

හරි එහෙනම් අපි දැන් බලමු අද පාඩම ගැන..අද පාඩම තමයි abstract class .
abstract class එකක් ගැන කියන්න ඉස්සරින් මං කියන්නම් abstract method එකක් කියන්නේ මොකක්ද කියලා…ඇත්තටම abstract කියන වචනයෙන්ම කියවෙනවා නේද “මොකුත් නෑ/හිස්” වගේ අදහසක්…ඒකම තමයි මේ class වලත් විශේෂත්වය…හැබැයි පොඩ්ඩක් වෙනස්…ඇත්තටම abstract method එකක් කියන්නෙ body එකක් නැති  එහෙමත් නැත්නම් meaningless body එකක් තියන method එකකට…abstract method එකක් define කරන්නේ method එක ඉදිරියෙන් abstract කියන වචනය දාලා…හරියට නිකන් මෙන්න මේ වගේ…

class Person {

     abstract void Jump(){

     }

}

ඔන්න ඔය විදියට තමයි abstract method එකක් define කරන්නේ…class එකක් ඇතුලේ ඔය වගේ abstract method එකක් හරි ඊට වැඩි ගානක් හරි තිබ්බොත් අන්න ඒ class එකත් abstract class එකක් වෙනවා… හරි එහෙනම් Person class එක ඇතුලෙ තියනවා නේද jump කියලා abstract method එකක් එහෙනම් අපේ Person class එකත් abstract වෙන්න ඕනි නේද…

abstract class Person {

         abstract void Jump(){

      }

}

ඔන්න ඔය වගේ class වලට තමයි abstract class එකක් කියන්නේ…abstract class මොකටද ගන්නේ කියලා අපි පසුව බලමු…දැනට මතක තියාගන්නකෝ abstract class එකක් කියන්නේ ඕන්න ඕකකට කියලා ☺☺..

මේ abstract class ගත්තොත් විශේෂතා ගොඩක් තියනවා….abstract method එකකනම් ඒකේ body එකේ මොකුත් තියා ගන්න බෑ …නමුත් abstract class එකක් ගත්තොත් ඒකේ abstract methods තියෙන්නත් පුලුවන් abstract නොවන methods තියෙන්නත් පුලුවන්…ඔන්න ඕක එක විශේෂත්වයක් abstract class වල තියෙන…තවත් දෙයක් තමයි මේ abstract class වලට objects හදන්න බෑ….object හදන්න බැහැ කියන්නේ constructor එකකුත් නැහැ..ඔන්න ඕක තවත් විශේෂත්වයක්…පොඩ්ඩක් මේ උදාහරණය දිහා බලන්නකෝ…

abstract class Person{

       abstract void jump();

       void run(){

       system.out.println(" i can run fast !");

       }

}

class Student extends Person{

       void study(){

       system.out.println("i am studding !");

       }

}

හරි එහෙනම් ඔය class දෙක දිහා හොදට බලන්නකෝ…Person class එකෙන් Student class එක inherit වෙලා තියනවා .. එහෙනම් Person class එකේ තියන privet නොවන සියලුම attributes හා methods Student class එක ඇතුලට එනවා නේද…ඒ කියන්නේ Person class එකේ තියන jump කියන abstract class එකත් Student class එක ඇතුලට ඇවිත් තියෙන්නේ…කලින් මං කිව්වනේ class එකක් ඇතුලේ අඩුම එක abstract method එකක්වත් තිබ්බොත් class එක abstract වෙනවා කියලා….එහෙනම් අපේ Student class එකත් මෙන්න මේ වගේ abstract කරන්න ඕනි නේද…

abstract class Person{

         abstract void jump();

         void run(){

         system.out.println(" i can run fast !");

         }

}

abstract class Student extends Person{

         void study(){

         system.out.println("i am studding !");

         }

}

හරි ඔන්න ඔහොම තමයි අපේ code එක එන්නේ…කිසි අව්ලක් නෑ…නමුත් මට ඕනි Student කෙනෙක්ගේ object එකක් හදන්න…Student class එක abstract එකක් නිසා මට Student කෙනෙක්ගෙ object එකක් හදන්න compiler එක ඉඩ දෙන්නේ නෑ…එහෙනම් කොහොමද මං Student කෙනෙක්ගේ object එකක් හදන්නේ…Student class එකේ තියන abstract කෑල්ල අයන් කරානම් හරි නේද …එහෙම කරන්නම් abstract methods class එකෙන් අයින් කරන්න ඕනි…Student Class එකේ තියන jump method එකනේ abstract method එකකට තියෙන්නේ…අන්න ඒ jump method එකේ abstract බව අයින් කලානම් හරි නේද…ඒ කියන්නේ jump method එක Student class එක ඇතුලෙදී override කලා නම් හරි නේද…override කලාම jump method එකට body එකක් තියන නිසා ඒ method එකේ abstract බව auto නැතිවෙලා යනවා ….code එක ලිව්වොත් මෙන්න මේ වගේ

abstract class Person{

         abstract void jump();

         void run(){

         system.out.println(" i can run fast !");

         }

}

class Student extends Person{

       void jump(){   

       system.out.println("i can jump !");        // overriding the abstract class

       }

       void study(){

       system.out.println("i am studding !");

       }

}

හරි දැන් මට අව්ලක් නැතිව Student object එක හදන්න පුලුවන් නේද……
abstract class වල තියන තව විශේෂත්වයක් තමයි , abstract class reference variable එකකට පුලුවන් sub class objects store කර ගන්න..(upcasting)..අපේ උදාහරණයේ විදියටනම් මෙන්න මේ දේ…

Person person1= new Student();

හරි ඔන්න ඔය ටික තමයි abstract class ගැන කියන්න තියෙන්නේ… interface වලදී abstract class හොදටම පාවිච්චි වෙනවා…එතකොට ඔයාලට තවත් හොදට තේරෙයි…

හරි අපි abstract class ගැන කතා කලානේ…දැන් මං කතා කරන්න හදන්නේ java වල තියෙන වැදගත් key word දෙකක් ගැන..කලින් අපි super keyword එක ගැනයි this keyword එක ගැනයි කතා කලනේ..අද කතා කරන්න යන්නේ final keyword එක ගැනයි static keyword එක ගැනයි…

final keyword :

ඇත්තටම කියනවනම් final keyword එකෙන් කරන්නේ variable එකකට නිශ්චිත constant value එකක් assign කරන එක…උදාහරණයක් විදියට ගත්තොත් වෘත්තයක ව.ඵ හොයද්දි අපට ඕනි වෙන £ =3.14 කියන අගය…අපිට ඒක final keyword එක දාලා මේ විදියට define කෙරුව හැකි.

final float £ = 3.14 ; 

ඔන්න ඕක තමයි final keyword එකෙන් කරන්නේ…

static keyword:

static keyword එකනම් ටිකක් වැදගත් keyword එකක්…අපිට static keyword එක දාලා attributes define කරන්නත් පුලුවන් methods define කරන්නත් පුලුවන්…attributes එකක් static කලොත් ඒ attribute එකේ value එක program එක අවසන් වුනත් නොවෙනස්ව පවත්වා ගන්න පුලුවන්..හරියට නිකන් මෙන්න මේ වගේ…

class Cat{

     int age;

     static int numOfSibligs=0;

     void run(){

          System.out.print("it can be run fast !");

     }

}

Class Demo{

      public static void main(String a[]){

              Cat cat1= new Cat(); // creating a cat object

              numOfSibligs ++;

              System.out.println("cat1 has "+ cat1.numOfSibligs +" sibligs");

              Cat cat2= new Cat(); //creating another object

              numOfSibligs ++ ;

              System.out.println("cat2 has "+ cat2.numOfSibligs +" sibligs");

      }

}

සාමාන්‍ය variable එකක නම් object එක එක පාරක් හැදුවහම එහි අගය lost වෙලා යනවා. නමුත් static keyword එක පාවිච්චි කලොත් එහෙම වෙන්නෙ නැහැ . out put එකකවත් පොඩ්ඩක් බලන්නකෝ.

Out put :

ඔන්න ඕකයි attribute එකක් static කලාම වෙන්නේ…දැන් අපි බලමු method එකක් static කලාම මොකද වෙන්නේ කියලා…ඇත්තටම method එකක් static කලාම , ඒ method එකට call කරන්න පුලුවන් object එකක් හදන්නේ නැතිව.ඒ කිව්වේ මෙන්න මේකයි.

class Cat{

     int age;

     static int numOfSibligs=0;

     static void run(){

          System.out.print("it can be run fast !");

     }

}

class Demo{

      public static void main(String a[]){

              Cat.run();

              

      }

}

output :

ඔන්න ඕක තමයි static keyword එකෙන් වෙන්නේ…

හරි අදට පාඩම හමාරි..😃😃…හැමෝටම තේරුනා කියලා හිතනවා…මොකක් හරි අව්ලක් තිබුනොත් පහලින් comment එකක් දාන්න…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 *