Thursday, September 27, 2012

OBJECT ORIENTED CONCEPTS IN JAVA (1 වන කොටස)



අද අපි කතා කරන්න යන්නේ  java වල එන ඉතා වැදගත් කොටසක් වන  object oriented සංකල්පය පිලිබදව. ඇත්තටම programming languages  ගත්තම ඒවා structural  හා  object oriented කියලා ආකාර දෙකක්  දකින්න පුලුවන්.  Java,  c++ වගේ  ඒවා object oriented සංකල්පය යොදා ගන්නා  programming languages  .  pascal,c වගේ  ඒවා structural සංකල්පය යොදා ගන්නා  programming languages  .

සරලව කිව්වොත්  object oriented concept එක කියන්නේ මොකක් හරි ප්‍රශ්නෙකට    විසදුම් සෙවීමේදී අපි එය real world situation  එකකට සමාන කොට  objects සාදා එම ගැටලුව විසදීමට උත්සාහ ගැනීමයි. .අපි  java  වලදී ඔනෑම programme  එකක් ලියන කොට  මුලින්ම කරන්නෙ class එකක් හදලා  එම එක තුල variables  සහ  methods  define කරලා එම class  එකෙන් අපිටඅවශ්ය පරිදි objects  සාදා  එම objects  සහ  methods බාවිතා කර  අපේ  program එකෙන් බලාපොරොත්තු වන  results එක ලබා ගැනීම. මෙන්න මේ සිද්දි දාමය තමා සරලවම කිව්වොත්  object oriented concept එක  java වලදී යෙදෙන අකාරය. දැන් අපි  java වලදී  object oriented concept එක යෙදෙන  අකාරය ගැන ගැබුරින් කතා කරමු.


Java වලදී  class එකක් කියන්නේ මොකක්ද ?

Class එකක් කියන්නේ object  එකක  මූලික සැලැස්ම නැත්නම් blue print  එකට තමා. object එකක ලක්ෂණ, හැසිරීම තීරණය  කරන්නේ  class එක මගින්. Class එකක් නොමැතිව අපිට objects  සාදන්න බැහැ. ඒ වගේම තමා අපි  class එකක්  define කරාට පස්සේ එම  class එක බාවිතා කර අපිට ඕනෑ තරම් objects  සාදන්න පුලුවන්.

Java වලදී   object  එකක්  කියන්නේ  මොකක්ද ?

Object එකක්  කියන්නේ  class  එකක එක් නිශ්චිත සිද්දියක් කියලා කියන්න පුලුවන්.(object is an instance of a class) ඕනම object  එකක් ගත්තම එයට අනන්‍ය  වුනු  data ටිකක් හා  methods ටිකක් තිබෙනවා.

Java වලදී  object  එකක් සාදා ගැනීම සදහා  syntax එක :

Classname  objectname(reference variable name )   = new  Classname( );

මේ අනුව objects  සැදීමේදී  new key word එක යොදා ගනී.
Ex:
 අපිට  student  කියන  class එකක්  භාවිතා කරලා std  කියල  object එකක් හදන්න ඔනි නම් එය කරන්නේ මෙහෙමයි.
Student   std1= new  Student();


අපිට  university  කියන class එකක් භාවිතා කරලා  colomboUni  කියල  object එකක් හදන්න ඔනි නම් එය කරන්නේ මෙහෙමයි.
 university   colomboUni= new university();
Objects සෑදීම ගැන වැඩි විස්තර  constructor කියන කොටස යටතේ දී කතා කරමු.

Java වලදී  constructor  එකක් කියන්නේ මොකක්ද ?

lass එකක් තුල ඇති variables,  objects  සෑදීම සදහා කෙසේ යොදා ගත යුතුද යන්න අපි  constructor එක මගින්  පෙන්වා දෙයි.

Java වලදී  constructor  එකක් සාදා ගැනීම සදහා  syntax එක :
 public   classname (parameter list ){
Constructor body;
}
වැදගත් :
·         Constructor  එකක් සැදීමේදී සැම විටම  public නම්  access modifier  එක භාවිතා කල යුතුයි.
·         ඉන් පසු  class එකේ නම යෙදිය යුතුයි.
·         ඇත්තටම  constructor එක කියන්නෙත්  එක්තරා අකාරයක  method එකක්.නමුත් මෙමගින් කිසිවක්  return කරන්නේ නැහැ .ඒ වගේම  constructor එක සෑදීමේදී  void කියන key word  එකවත් අපි භාවිතා කරන්නේ නැහැ.
·         Class එකක් සදහා අපිට  constructors එකක් හෝ එකට වඩා වැඩි ගනනක්  වුවද සෑදිය හැකියි. එසේම අපිට අවශ්ය නම් constructors  එකක් වත් නොසාදා වුවද සිටිය හැකියි.
·         Parameter list එක තුලදී  class  එක තුල ඇති variables  වලින් අපි  object එක සෑදීමට යොදා ගන්නේ කුමන variables දැයි දැක්වි යුතුයි. එම  variables වල නම් අපි class  එක තුල  define කර ඇති ආකාරයෙන්ම හෝ නැතිනම් වෙනත් ආදේශක නම් වලින් දැක් විය යුතුයි. Class එක තුල  define කර ඇති ආකාරයෙන්ම  එම  variables වල නම් අපි parameter list  එක තුල දක්වයි නම්  constructor   එක තුලදී අපිට this key word  එකද භාවිතා කිරීමට සිදුවෙයි.
·         අපි විසින් සැදිය යුතු  යම්  object එකක් සදහා  class  එක තුල පිහිටි යම් variable  එකක අගය නියතව තබා ගත යුතු නම්  constructor එක තුලදී එම  variable එකේ අගය නියත කල යුතුය.
·         Constructor එක තුලදී  parameters pass  කරයි නම් එම constructor  එක භාවිතා කොට objects  සැදීමේදී එම parameters  සදහා  values pass කල යුතුය.

දැන් අපි class  එකක් භවිතා කොට  constructors සාදන ආකාරය බලමු .

class එකේ නම StudentDetails  ලෙසත් එහි ඇති  variables ,
private String name;
private  int age;
private  String  universityName;  ලෙසත් සිතමු.
දැන් මෙම  class එක භවිතා කොට සාදා ඇති පහත  constructors   බලන්න.

public   StudentDetails  ( String  a , int  b, String  c ){
            name=a;
            age=b;   
universityName=c;
}

මෙම  constructor එක සැදීමේදී  class එකේ ඇති String name  , int age ,String universityName යන  variables වෙනුවට parameter list එක තුලදී  එම  variables පිලිවෙලින්  String a,  int b, String c යනුවෙන්  define  කර ඇත. එසේම මෙම constructor  එක  සැදීමේදී  class එක තුල ඇති සියලුම variables  අපි භාවිතා කර ඇත.

public   StudentDetails  ( String a , String c ){
            name=a;
            universityName=c;
}

මෙම  constructor එක සැදීමේ දී class  එක තුල පිහිටි  variables දෙකක් පමනක් යොදා ගෙන ඇත.

public   StudentDetails  ( String a , String c ){
            name=a;
            universityName=c;
            age=22;
}

මෙම  constructor එකේ ඇති විශේෂත්වය නම් මෙය භාවිතා කර අපි සාදන objects  වලදී   age නම්  variable  එකේ අගය  සැම  object එකක් සදහාම  22 ලෙස නියතව තබා ගැනීමයි. ඒ සදහා අපි මෙම constructor  එකේ  constructor body එක තුලදී age=22 ලෙස define  කර ඇත.

public   StudentDetails  ( String  name ,  int  age,  String   universityName){
            this.name=name;
            this.age=age;
            this.universityName= universityName;
}

මෙම  constructor එකේ  parameter list එක තුලදි අපි   variable names දක්වා ඇත්තේ  class එක තුලදී එම  variables define කර ඇති නම් වලින්මය. මෙසේ  variable names ,parameter list එක තුල  දක්වයි නම් මෙහි දක්වා ඇති පරිදි  this key word එක භාවිතා කල යුතුයි.

this.name=name;  මින් පවසන්නේ parameter   එකක් ලෙස ලබා   දෙන name හි අගය මෙම  object එකට අයත්  name variable එකට  assign කරන ලෙසයි. මෙහිදී  this key word එක භාවිතා නොකල හොත් අපි  parameter එක ලෙස ලබා දෙන අගය  object එක සැදීමේදී යොදා නොගන්නා අතර  object  එක සැදීමේදී instance variables  වල අගයන් භාවිතා කරයි.( Instance variables යනු අපි  class එක සැදීමේදී මුලින්ම  define  කරන  variables ය. මෙම එකට අදාලව private String name, private  int age , private  String  universityName  යන ඒවා.)

දැන ගත යුතු තවත් වැදගත්   දෙයක් තමා  java  වලදී අපි සාදන සැම  class එකක් සදහාම   constructors  සැදිය යුතු නැහැ.   Constructor එකක් නොසාදා objects  වලට  values pass කිරීම අපට  methods භාවිතයෙන් වුනත් කරන්න පුලුවන්. නමුත්  constructor එකක් නොසැදුවත්   එම  programme වලදී default constructor   එකක්  run වෙනවා. උදාහරනයක් ලෙස අපි ඉහත  students class එකට constructor  එකක් නොසැදුවේ නම් එම class  එක භාවිතා කර  Student s1=new Student( )ලෙස  object එකක් සැදීමේ දී පහත default constructor එක  run වීම සිදු වනවා.

public Student( ){
name=null;
age=0;
universityName=null;
}

මෙහිදී  variables වලට assign  කර ගන්නේ  instance variables වල අගයන්ය. අපි  student class එක තුලදී instance variables  සදහා values assign කර නොමැති නිසා integer variables  සදහා 0  ද String variables  සදහා null ලෙසද  values  assign කර ගනී. 


Java වලදී   method   එකක් කියන්නේ මොකක්ද ?

Java වලදී  methods භාවිතා කරන්නේ අපි අවශය වැඩ කරවා ගැනීමටයි. Methods සෑදීම මගින් කේත නැවත නැවත අපිට අවශ්ය ස්තාන වලදී භාවිතයට යොදා ගත හැක. Method එකක් ක්‍රියත්මක වන්නේ එය  call කල විට පමනි.එතෙක් එය තුල ඇති උපදෙස් ක්‍රියාත්මක නොවේ.
දැන් අපි බලමු method  එකක් සාදා ගැනීම සදහා syntax  එක :

Access modifier   return type  method name(parameter list){
Method body
}

Methods සෑදීමේදී යොදා ගන්නා access modifiers :

public : method එකක් මෙම access modifier එක සහිතව  declare කල හොත් එම method  එක ඕනෑම   class එකකදී භාවිතා කල හැක.

private  : method එකක් මෙම access modifier එක සහිතව  declare කල හොත් එම method  එක භාවිතා කල හැක්කේ  එම  method  එක declare  කරන  class එක තුලදී පමනි.

protected : method එකක් මෙම access modifier එක සහිතව  declare කල හොත් එම method  එක භාවිතා කල හැක්කේ  එම  method  එක declare  කරන  class එක තුලදී හා එහි  sub classes තුලදී පමනි. (sub classes ගැන පසුව ඉගෙන ගනිමු. )

default modifier : යම්  method එකක් සදහා access modifier කෙනෙක් යොදා නොමැති විට එයට යෙදෙන්නේ  default modifier එකයි. method එකක් මෙම access modifier එක සහිතව  declare කල හොත් එම method  එක  භාවිතා කල හැක්කේ  එම  method  එක declare  කරන  class එක තුලදී හා එම class  එක අයත්  package එක තුලදී පමනි.

Method එකක් return type  එක කියන්නෙ මොකක්ද :

Method එකක් මගින් යම් කිසි වැඩක් සිදු කර අවසානයේදී මොකක් හරි  value එකක්return  කරනව නම් එම  value  එක අයිති  data type එකට තම  method එකේ return type එක කියන්නේ. ඒ කියන්නේ method එකෙන් අවසානයේදී  int value එකක්  return කරනවා නම්  method එකේ return type එක වෙන්නේ int. method එකක් මගින් සෑම විටම  value එකක් return කල යුතු නැහැ.යම් වැඩක් සිදු කොට අවසානයේදී  value එකක්  return නොකරන  methods තියෙන්නත් පුලුවන්.එවැනි method  එකක  return type එක වෙනුවට අපි යොදන්නේ void  කියන key word  එක.

Method name එක කියන්නේ  method එක හදුනා ගැනීම ස්දහා අපි විසින්   method එකට දෙනු ලබන නම.

Method  එකක  parameter list එක තුලදී  method එක සෑදීම සද්හා යොදා ගනු ලබන  instance variables හදුන්වා දිය යුතුයි.අපි  constructor එකක් සාදන විටත් මෙය සිදු කලා ඔබට මතක ඇති.  සෑම  method එකක් සදහාම parameters  දිය යුතු නැහැ. Parameters ඇතිව වගේම නැතුවත් අපිට  methods ලියන්න  පුලුවන්. ඒ වගේම තමා යම්  method එකක්  declare කරද්දී එයට  parameters ලබා දී ඇත්නම් එම  method එක  call කරන විට  parameters සදහා  values pass කල යුතුයි.

Method body එක තුලදී අපි method එක භාවිතයෙන් කර ගැනීමට බලාපොරොත්තුවන වැඩ සදහා  code ලිවීම සිදු කරනවා. එසේම  method එක මගින් අදාල වැඩ සිදු කොට යම් කිසි  value එකක්  return කරයි නම් එ ය  return key word  එකත් එක්ක දැක්විය යුතුයි.

Method එකක්  call කිරීම.

Method එකක් call  කිරීමට නම් පලමුව එම  method එක declare  කර ඇති class එකෙන් object එකක් සෑදිය යුතුය. අනතුරුව පහත දැක්වෙන පරිදි එම object  එක හා . operator එක භවිතා කොට method එක call කල හැක.
Objectname.methodname( );
මෙහිදී  object එකේ නම හා  method එකේ නම අතර   .  operator  එක භාවිතා කරයි.

අපි යම්කිසි  method එකක්  static key word එක සහිතව  declare කර ඇත්නම් එම  method එක පිහිටි  class එක තුලදී එම  method එකට  call කිරිම  object එකක්  නොසාදා වුවද කල හැක. එයට හේතුව යම් method  එකක් static key word එක සහිතව  declare කල විට එම method  එක එම  class එකේ  objects සියල්ලටම පොදු වීමයි. එනම් පහත syntax එකේ  පරිදි  method එක  call කල හැක.
methodname( );

එසේම static key word  එක සහිතව  declare කර ඇති  method එකක් වෙනත් class  එකක් තුලදී  call කරන විට  objects නොසාදා එම  method එක පිහිටි  class එකේ නමින් ද පහත syntax එකේ  පරිදි  call  කල හැක.
classname.methodname();

දැන්  methods ගැන කතා කලා ඇති කියල හිතනවා.මීලගට  object oriented concept එක යොදා ගෙන  java programme එකක් ලියන විට වැදගත් වන තවත් කරුනු කීපයක් මතක් කරන්න්නම්. 


වැදගත් :

v  Java වලදී  objects සෑදීම සදහා  class එකක් ලියන විට අපි   පහත ආකාරයේ පොදු structure   එකක් භාවිතා කරනවා.

class classname  {

[ variables  හදුන්වා දීම ]

[ constructos සෑදීම  ]

[ methods  සෑදීම ]

}
  
v  Object orientes  programme  එකක   instance variables (attributes ) , private ලෙසත්  methods , public  ලෙසත් සාමාන්යයෙන්  declare කරයි.  මෙයට හේතුව  instance variables වල අගයන් වෙනත් class  එකකදී වෙනස් කිරීමට නොහැකි වන ලෙස දැක්වීමටත්  methods  වෙනත් class  තුලදී භාවිතා කල හැකි ලෙස දැක්වීම සදහාත්ය. කෙසේ වුවද ඔබට අවශ්ය නම්  instance variables , private access modifier   එක රහිතව වෙනත් access modifier   එකක් සමගත් methods, public access modifier එක  රහිතව වෙනත් access modifier  එකක් සමගත්  declare කල හැක.

v  එසේම ඔබට යම් වැඩක් සිදු කර ගැනිම සදහා class  කිහිපයක් සාදා ගත යුතු යයි සිතන්න.එවිට ඔබ එම class සියල්ල්ලම එකම තැනක  save කර ගත යුතුයි. (එනම් එකම folder  එකක් තුල )එසේ නොමැතිව වෙන් වෙන් ස්තාන වල එක් එක්  class එක save  කල හොත්  programme එක නිසි පරිදි ක්‍රියා නොකරනු ඇත.  එසේම මෙලෙස class කිහිපයක් ඇති විට ඔබ  compile කල යුත්තේ main  method  එක අඩන්ගු  class  එකයි.

v  ඒ වගේම තවත් වැදගත් දෙයක් කියන්න ඕන. Java වලදි අපි  student කියන class එක භාවිතා කරලා  st1 කියල  object එකක් පහත පරිදි හැදුව කියමුකො.

student stu1=new student( );

මෙවිට ඇත්තටම සිදු වනේ  stu1  කියල  object  එකක්  computer memory එකේ සෑදීම නෙවෙයි.  Memory එක තුල  object එකක් හැදිලා එය සෑදුනු තැන  memory address එක මෙම  stu1 කියන  reference variable එකේ ගබඩා වීම. එනම් java  variable   එකක් තුල ඇත්තේ  object   එකක්  ram එක තුල   පවතින ස්තානයට අදාල  memory address එකකි.

අද අපි object oriented   ගැන ගොඩක්  theory ඉගෙන ගත්තනේ. දැන් අපි එම කරුනු තහවුරු කර ගැනීම සදහා පහත  programs  run කර බලමු. මෙම programme එකේ class දෙකක් ඇති නිසා මතක ඇතුව එම class දෙකම එකම folder  එකක save කර ගන්න.

public class Student {
           
            // these are instance variables.
           
            private String studentName;
            private int yearOfBirth;
            private String village;
            private String university;
           
           
            // this is a constructor.
           
            public Student (String a,int b,String c,String d){
                        studentName=a;
                        yearOfBirth=b;
                        village=c;
                        university=d;
                       
            }
           
           
           
           
            /*this is a method. by that method we calculate student's age by years.for this method we have passed two parameters.*/

           
            public void  getAgeByYears(int yearOfBirth ,String studentName ){
                       
                        int age=2012-yearOfBirth;
                        System.out.println("Age by years of "+ studentName+ "  is " +age);
            }


           
            // this is a method which returns student's name

           
            public String getstudentName(){
                        return studentName;
                       
            }

           
            // this is a method which returns student's village.

           
            public String getvillage(){
                        return village;
            }

           
            /*this is also a method. but this method doesn't return anything. by this method we can print student's university name.*/

            public void getuniversity(){
                        System.out.println(university);
            }

}



Code 2:-

public class StudentTest {

           
            public static void main(String[] args) {
                       
                        // here we have created four student objects using Student  class.
                        /*when creating objects we have passed values  for the parameters in student constructor. */
                       
                        Student st1=new Student("hasitha" ,1990 , "matara", "colombo ");
                        Student st2=new Student("kamal" ,1989 , "galle", "moratuwa ");
                        Student st3=new Student("saunil" ,1991 , "gampaha", "kelaniya ");
                        Student st4=new Student("gayan" ,1987 , "hambanthota", "ruhuna ");
                       
                        // here we call the getuniversity method for st1 object.

                       
                        st1.getuniversity();

                       
                        /* here we call the getAgeByYears method for st2 object.when call this method we have passed values for parameters in the method definition.*/
                       
                       
                        st2.getAgeByYears(1989,"kamal");
                       
                       
                        /*in the below code segment I have called getstudentName method and getvillage method for student objects.*/
                       
                        System.out.println("");
                        System.out.println("Names  and villages of students : ");
                        System.out.println("");
            System.out.println(st1.getstudentName()+ "  is from   "+st1.getvillage() );
            System.out.println(st2.getstudentName()+ "  is from   "+st2.getvillage() );
            System.out.println(st3.getstudentName()+ "  is from    "+st3.getvillage() );
            System.out.println(st4.getstudentName()+ "  is from   "+st4.getvillage() );
                       
           
                       
                       
                       
                       
            }

}


මෙහි  output  එක         :











 දැන්  constructor තුලදී this key word  එක යොදා ගන්නා අකාරය සහ class  එකක් තුල දී  constructor එකකට වඩා සාදා ගන්නා ආකාරය දැක්වෙන පහත programme   එක run  කර බලන්න.


public class Shop {
             
            private String itemName;
            private float price;
            private String companyName;
           
            / *In this constructor I have used instance variable names in  parameter list .So I must use this key word in the constructor.*/
           
            public Shop(String itemName,float price,String companyName){
                        this.itemName=itemName;
                        this.price=price;
                        this.companyName=companyName;
                       
            }

           
            /* here I have created another constructor for this class. we can create any number of constructoers for a class.*/
           
           
            public Shop(String item){
                        itemName=item;
                       
            }
           
           
           
            public void getItemDetails(){
                        System.out.println("itemName = " + itemName  + " price = " + price +"  companyName = "+ companyName);
            }

           
            public void getItemName(){
                        System.out.println("ItemName : "+itemName );
            }
           

           
            public static void main(String[] args) {
                       
                        //I have created  it1,it2,it3 objects  using first constructor.
                       
                        Shop it1=new Shop("cream soda ", 47.50f," elephant house");
                        Shop it2=new Shop("sun light  ", 55.75f," uniliver ");
                        Shop it3=new Shop("highland milk ", 275.0f," milko ");
                       
                        // for creating it4, it5 objects  I have used second constructor.
                       
                        Shop it4=new Shop("cream cracker");
                        Shop it5=new Shop("ice cream ");
                       
                       
                        it1.getItemDetails();
                        it2.getItemDetails();
                       
                        System.out.println("");
                       
                        it4.getItemName();
                        it5.getItemName();
                       
                       
                       
                       
            }

}

මෙහි  output  එක :





                       
           
Constructor එකක්  define   නොකර java class  එකක් සාදා ගන්නා ආකාරය දැක්වෙන පහත  programme එකත්  run කර බලන්න.(මෙවිට default constructor  එකක් run  වේ.)


public class Computer {
            private  String displayType;
            private  String processorType;
            private String manufactror;
           
           
            public void setdisplayType(String disType){
                        displayType=disType;
                       
            }
           
            public void setprocessorType(String proType){
                        processorType=proType;
                       
                       
                       
            }
           
            public void setManufactro(String manufactror){
                        this.manufactror=manufactror;
                       
            }
           
           
            public void getDetails(){
                        System.out.println("This is a "+ manufactror+ " computer with "+  processorType+"  processor and with  a  " +displayType+ " display");
            }
           
           
           

           
            public static void main(String[] args) {
                       
                        Computer c1=new Computer();
                        Computer c2=new Computer();
                        Computer c3=new Computer();
                       
                       
                        c1.setManufactro("HP");
                        c1.setprocessorType("i3");
                        c1.setdisplayType("LCD");
                       
                        c2.setManufactro("DELL");
                        c2.setprocessorType("i5");
                        c2.setdisplayType("LED");
                       
                        c3.setManufactro("IBM");
                        c3.setprocessorType("P3");
                        c3.setdisplayType("CRT");
                       
                        c1.getDetails();
                        c2.getDetails();
                        c3.getDetails();
                       
                       
                       
                       
                       

            }

}

මෙහි  output  එක :










 එහෙනම් අදට නවතිමු.ලගදීම තවත්  post එකකින් හමු වෙමු.අද කියපු දේවල් ගැන ගැටලු තියනව නම් comment කරන්න.නැත්නම් අපිට email  කරන්න.


By Hasitha Thamaranga