အခြေခံ ကွန်ပျူတာပရိုဂရမ် ရေးသားနည်း - Objected-Oriented Programming

Object-Oriented Programming 

Object-Oriented ဆိုတဲ့ အိုင်ဒီယာဟာ Software တွေကို modelling လုပ်ရင်းနဲ့ ထွက်ပေါ်လာတဲ့ အိုင်ဒီယာ တစ်ခု ဖြစ်တယ်။ အပြင် တကယ်လက်တွေ့လောကမှာ ကားတွေ လေယာဉ်တွေ အဆောက်အဉီးတွေ  အစရှိတဲ့အရာတွေရဲ့ တည်ဆောက်ကြတဲ့ သဘောသဘာဝကို အတုယူတာ ဖြစ်တယ်။


Intel ဟာ Intel Processor ထုတ်တယ်။ Seagate ဟာ Hard Disk Drive တွေထုတ်တယ်။ MSI ဟာ Motherboard ထုတ်တယ်။ Dell ကွန်ပျူတာဟာ Intel Processor ကို အသုံးပြုသလို Lenovo laptop မှာလည်း Intel Processor ကို အသုံးပြုကြတယ်။ 


Intel Processor ဟာ Object တစ်ခုဖြစ်တယ်။ Dell ကွန်ပျူတာဟာလည်း ​Object တစ်ခုဖြစ်တယ်။ Object တစ်ခုကို တခြား Object တစ်ခုမှာ ယူသုံးလို့ရတယ်။ 


အဲ့ဒီနည်းလမ်း Object-Oriented ဟာ Software တွေ ရေးသားရာမှာ အလုပ်ခွဲဝေ လုပ်ကိုင်ဖို့အတွက် အလွန် ကောင်းမွန်တဲ့ နည်းလမ်းတစ်ခု ဖြစ်တယ်။

Using Data Types

Class တစ်ခုဖြစ်တဲ့ String ဟာ Non-primitive data type ဖြစ်သလို java.awt.Color ထဲက Color ဟာလည်း non-primitive data type တစ်ခု ဖြစ်တယ်။ String နဲ့ Color အစရှိတဲ့ class တွေကနေ String Object နဲ့ Color Object တွေ တည်ဆောက်လို့ ရတယ်။ 

Color

Java ရဲ့ color data type ဟာ Red, Green, Blue (RGB) ဆိုတဲ့ integer သုံးခုကို အသုံးပြုပြီး color တွေရဲ့ အရောင် အရင့် အဖျော့ စသည်တို့ကို သတ်မှတ်ပေးတယ်။   


For example : 

Color red      = new Color(255,   0,   0);

Color bookBlue = new Color(  9,  90, 166);

Example of Color Data Type

/*******************************************************************

 *  Compilation:  javac AlbersSquares.java

 *  Execution:    java AlbersSquares r1 g1 b1 r2 g2 b2

 *  Dependencies: StdDraw.java

 *  

 *  This program displays the colors entered in RGB format

 *  on the command line in the familiar format developed

 *  in the 1960s by the color theorist Josef Albers that

 *  revolutionized the way that people think about color.

 *

 *  % java AlbersSquares 0 174 239  147 149 252

 *

 *******************************************************************/


import java.awt.Color;

public class AlbersSquares {

    public static void main(String[] args) {

        StdDraw.setCanvasSize(800, 800);


        // first color

        int r1 = Integer.parseInt(args[0]);

        int g1 = Integer.parseInt(args[1]);

        int b1 = Integer.parseInt(args[2]);

        Color c1 = new Color(r1, g1, b1);


        // second color

        int r2 = Integer.parseInt(args[3]);

        int g2 = Integer.parseInt(args[4]);

        int b2 = Integer.parseInt(args[5]);

        Color c2 = new Color(r2, g2, b2);


        // first Albers square

        StdDraw.setPenColor(c1);

        StdDraw.filledSquare(0.25, 0.5, 0.2);

        StdDraw.setPenColor(c2);

        StdDraw.filledSquare(0.25, 0.5, 0.1);


        // second Albers square

        StdDraw.setPenColor(c2);

        StdDraw.filledSquare(0.75, 0.5, 0.2);

        StdDraw.setPenColor(c1);

        StdDraw.filledSquare(0.75, 0.5, 0.1);

    } 

}

Creating Data Types

Java မှာ custom data type သို့မဟုတ် user defined data type တွေကို ရေးသားဖန်တီးနိုင်ပါတယ်။ 


Dell Laptop ဟာ Lenovo Laptop နဲ့ယှဉ်ပြီး ပါဝင်တဲ့ features တွေ အနည်းငယ် ကွာခြားနိုင်ပါတယ်။ ဒါပေမဲ့ Dell ဟာ Laptop အမျိုးအစားဖြစ်သလို ​Lenovo ဟာလည်း ​Laptop အမျိုးအစား ဖြစ်တယ်။ Object-Oriented အရ translate လုပ်မယ်ဆိုရင် ​Laptop ဟာ ​Class ဖြစ်ပြီး Dell Laptop နဲ့ Lenovo Laptop တွေဟာ Laptop Class ကနေ instantiate လုပ်ထားတဲ့ objects တွေ ဖြစ်တယ်။ 

Class

Class ဟာ object တွေကို create လုပ်ပေးနိုင် member data နဲ့ methods တွေပါဝင်တဲ့ design သို့မဟုတ် blueprint တစ်ခုဖြစ်တယ်။


ဉပမာ။ ။ ကားမှာ ဘီး လေးဘီး ရှိပြီး အရှေ့သွားခြင်း အနောက်ပြန်ဆုတ်ခြင်း ပြုလုပ်နိုင်တယ် ဆိုကြပါစို့။ ကားရဲ့ ဘီးလေးဘီးဟာ member data ဖြစ်ပြီး အရှေ့သွားခြင်း နောက်ပြန်ဆုတ်ခြင်းတွေကတော့ method တွေ ဖြစ်တယ်။ 

Class Example

public class Website {

   //fields (or instance variable)

   String webName;

   int webAge;


   // constructor

   Website(String name, int age){

      this.webName = name;

      this.webAge = age;

   }


   public static void main(String args[]){

      //Creating objects

      Website obj1 = new Website("softeracomputer", 5);

      Website obj2 = new Website("amazon.com", 18);


     //Accessing object data through reference

     System.out.println(obj1.webName+" "+obj1.webAge);

     System.out.println(obj2.webName+" "+obj2.webAge);

   }

}

Access Modifiers

အိမ်ရှေ့မှာ ထားလေ့ရှိကြတဲ့ ရေအိုးစင်တစ်ခုဟာ အများသုံး(public access) အတွက် ရည်ရွယ်တာ ဖြစ်ပါတယ်။ အိမ်ထဲမှာ ရှိတဲ့ ဝါတာကူလာကိုတော့ မိမိတို့ မိသားစု နဲ့ ဆွေမျိုး အပေါင်းအသင်းတွေ အသုံးပြုဖို့ (protected access) အတွက် ရည်ရွယ်တာ ဖြစ်တယ်။ သွားတိုက်တံလို ပစ္စည်းမျိုးကိုကျတော့ မိမိတဉီးတည်း (private access) အတွက် အသုံးပြုဖို့ဖြစ်ပါတယ်။ 


အဲ့ဒီ​ သဘောသဘာဝ အတိုင်း ​Java မှာ variable, method, class စသည်တို့ အတွက် access တွေကို ကန့်သတ်ပေးလို့ ရပါတယ်။ 


Default Access Modifier

Variable, method, class စသည်တို့ရဲ့ရှေ့မှာ ဘာမှ မကြေငြာထားရင် Default Access Modifier လို့ ခေါ်ပါတယ်။ Java Package အတွင်းမှာ အသုံးပြုနိုင်ပါတယ်။


Private Access Modifier

Variable, method, class စသည်တို့ရဲ့ရှေ့မှာ private လို့ ကြေငြာထားရင် Private Access Modifier လို့ ခေါ်ပါတယ်။ Class ရဲ့ အတွင်းမှာပဲ အသုံးပြုနိုင်ပါတယ်။


Protected Access Modifier

Variable, method, class စသည်တို့ရဲ့ရှေ့မှာ protected ကြေငြာထားရင် Protected Access Modifier လို့ ခေါ်ပါတယ်။ Java Package အတွင်းမှာ အသုံးပြုနိုင်သလို အမွေဆက်ခံတဲ့ Java Package ကနေလည်း အသုံးပြုနိုင်ပါတယ်။


Public Access Modifier

Variable, method, class စသည်တို့ရဲ့ရှေ့မှာ public ကြေငြာထားရင် Public Access Modifier လို့ ခေါ်ပါတယ်။ Class တိုင်း Java Package တိုင်းကနေ အသုံးပြုနိုင်ပါတယ်။

The scope of access modifiers 

 | Class | Package | Subclass     | Subclass     |Outside|

            |       |         |(same package)|(diff package)|Class  |

————————————+———————+—————————+——————————----+—————————----—+————————

public      | Yes   |  Yes    |    Yes       |    Yes       |   Yes |    

————————————+———————+—————————+—————————----—+—————————----—+————————

protected   | Yes   |  Yes    |    Yes       |    Yes       |   No  |    

————————————+———————+—————————+————————----——+————————----——+————————

default     | Yes   |  Yes    |    Yes       |    No        |   No  |

————————————+———————+—————————+————————----——+————————----——+————————

private     | Yes   |  No     |    No        |    No        |   No  |

------------+-------+---------+--------------+--------------+--------

Object

BMW ဟာ Car အမျိုးအစားဖြစ်သလို Toyota ဟာလည်း Car အမျိုးအစား ဖြစ်တယ်။ Object-Oriented အရ translate လုပ်မယ်ဆိုရင် Car ဟာ ​Class ဖြစ်ပြီး BMW နဲ့ Toyota တွေဟာ Car Class ကနေ instantiate လုပ်ထားတဲ့ objects တွေ ဖြစ်တယ်။ 


Java မှာ Class ကနေ new keyword နဲ့ instantiate လုပ်ပြီး object တွေကို ဖန်တီးလို့ ရပါတယ်။ 

Object Example

/********************************************************************

 *  Compilation:  javac PhoneNumber.java

 *  Execution:    java PhoneNumber

 *

 *  Immutable data type for US phone numbers.

 *

 ********************************************************************/


public final class PhoneNumber {

    private final int area;   // area code (3 digits)

    private final int exch;   // exchange  (3 digits)

    private final int ext;    // extension (4 digits)


    public PhoneNumber(int area, int exch, int ext) {

        this.area = area;

        this.exch = exch;

        this.ext  = ext;

    }


    // how you're supposed to implement equals

    public boolean equals(Object y) {

        if (y == this) return true;

        if (y == null) return false;

        if (y.getClass() != this.getClass()) return false;

        PhoneNumber that = (PhoneNumber) y;

        return (this.area == that.area) && (this.exch == that.exch) && (this.ext == that.ext);

    }


    // 0 for padding with leading 0s

    public String toString() {

        return String.format("(%03d) %03d-%04d", area, exch, ext);

    }


    // satisfies the hashCode contract

    public int hashCode() {

        return 10007 * (area + 1009 * exch) + ext;

    }


    public static void main(String[] args) {

        PhoneNumber a = new PhoneNumber(609, 258, 4455);

        PhoneNumber b = new PhoneNumber(609, 876, 5309);

        PhoneNumber c = new PhoneNumber(609, 203, 5309);

        PhoneNumber d = new PhoneNumber(215, 876, 5309);

        PhoneNumber e = new PhoneNumber(609, 876, 5309);

        StdOut.println("a = " + a);

        StdOut.println("b = " + b);

        StdOut.println("c = " + c);

        StdOut.println("d = " + d);

        StdOut.println("e = " + e);

        StdOut.println("b == b:      " + (b == b));

        StdOut.println("b == e:      " + (b == e));

        StdOut.println("b.equals(b): " + (b.equals(b)));

        StdOut.println("b.equals(e): " + (b.equals(e)));

    }

}

Constructor

Class တစ်ခုမှာ member data နဲ့ methods တွေပါဝင်တယ်။ Class ကနေ object တစ်ခုကို new keyword နဲ့ instantiate လုပ်တဲ့အခါမှာ တခါတည်းပါသွားစေချင်တဲ့ ဒေတာ value တွေကို Constructor ထဲမှာ ကြေငြာလို့ရတယ်။


ဆိုလိုတာက object တစ်ခုကို create လုပ်တဲ့အခါ အရင်းဉီးဆုံး Constructor ကို အရင် execute လုပ်တယ်။ Default Constructor နဲ့ Parameterized Constructor ဆိုပြီးရှိတယ်။ Default Constructor နဲ့ fit မဖြစ်တဲ့ ကိစ္စတွေမှာ Parameterised Constructor ကို အသုံးပြုလေ့ရှိတယ်။

Constructor Example

public class ConstructorExample {

   int age;

   String name;


   //Default constructor

   ConstructorExample(){

this.name="DIL BAHADU";

this.age=18;

   }

   //Parameterized constructor

   ConstructorExample(String n,int a){

this.name=n;

this.age=a;

   }


   public static void main(String args[]){

ConstructorExample obj1 = new ConstructorExample();

ConstructorExample obj2 = 

      new ConstructorExample("Mathew", 56);

System.out.println(obj1.name+" "+obj1.age);

System.out.println(obj2.name+" "+obj2.age);

   }

}

Encapsulation

အထဲမှာ အမှုန့်လေးတွေပါတတ်တဲ့ ဆေးတောင့် capsule လေးတွေကို ကျွန်တော်တို့တတွေ သောက်ဖူးလေ့ရှိကြပါတယ်။ 


Class တစ်ခုမှာ member variable တွေကို hide လုပ်ပြီး create လုပ်ထားတာကို encapsulation လို့ ခေါ်တယ်။

Encapsulation Example

Instance variable/member variable တွေကို client class တွေကနေ တိုက်ရိုက် ခေါ်လို့မရအောင် private လို့ ပေးရပါတယ်။ 


Client တွေ အသုံးပြုဖို့ အတွက်ကြတော့ Getter နဲ့ Setter method တွေကို provide လုပ်ပေးနိုင်တယ်။


class EmployeeCount

{

   private int numOfEmployees = 0;


   public void setNoOfEmployees (int count)

   {

       numOfEmployees = count;

   }

   public double getNoOfEmployees () 

   {

       return numOfEmployees;

   }

}

public class EncapsulationExample

{

   public static void main(String args[])

   {

      EmployeeCount obj = new EmployeeCount ();

      obj.setNoOfEmployees(5613);

 System.out.println("No Of Employees: "+(int)obj.getNoOfEmployees());

    }

}

Inheritance

​​Java class တစ်ခုမှာ ပါဝင်တဲ့အရာတွေကို တခြား class တစ်ခုက အမွေဆက်ခံလို့ရတယ်။ အခြေခံကျတဲ့ functionalities တွေကို parent class/super class ထဲမှာ ရေးပြီး unique ဖြစ်တဲ့ functionalities ကို အမွေဆက်ခံတဲ့ child class/subclass မှာ ရေးလို့ရတယ်။ 


Java မှာ Multiple Inheritance ကို support မလုပ်ပါ။

Inheritance Example

class Teacher {

   String designation = "Teacher";

   String college = "SofteraComputer";

   void does(){

System.out.println("Teaching");

   }

}

public class MathTeacher extends Teacher{

   String mainSubject = "Maths";

   public static void main(String args[]){

      MathTeacher obj = new MathTeacher();

      System.out.println(obj.college);

      System.out.println(obj.designation);

      System.out.println(obj.mainSubject);

      obj.does();

   }

}

Polymorphism

​​ပိုလီမော်ဖိဆင်ဟာ action တစ်ခုကို နည်းလမ်းများစွာနဲ့ လုပ်လို့ရစေတဲ့ Object-Oriented ရဲ့ feature တစ်ခု ဖြစ်တယ်။  


Animal ဆိုတဲ့ class တစ်ခုမှာ AnimalSound() ဆိုတဲ့ method ရှိတယ် ဆိုပါစို့။ အဲ့ဒီ class ကို Dog Cat အစရှိတဲ့ ဘယ် class က inherits/extends အမွေဆက်ခံမလဲဆိုတာ ကြိုတင် မသိနိုင်ဘူး။ Dog ဆိုတဲ့ class အတွက် AnimalSound() ဟာ တစ်မျိုး Cat ဆိုတဲ့ class အတွက် AnimalSound() ဟာ တစ်မျိုး ဖြစ်တယ်။ 


Animal ဆိုတဲ့ class ကို အမွေဆက်ခံတဲ့ class တိုင်းက AnimalSound() ဆိုတဲ့ method ကို implement လုပ်လို့ရစေတဲ့ mechanism ကို polymorphism လို့ ခေါ်တယ်။


Static Polymorphism နဲ့ Dynamic Polymorphism ဆိုပြီး နှစ်မျိုးရှိတယ်။ ​Compile time မှ  resolve လုပ်တဲ့နည်းကို Static Polymorphism လို့ခေါ်ပြီး Runtime မှာ resolve လုပ်ရင် Dynamic Polymorphism လို့ခေါ်တယ်။


public abstract class Animal{

   ...

   public abstract void animalSound();

}


public class Lion extends Animal{

...

    @Override

    public void animalSound(){

        System.out.println("Roar");

    }

}


public class Dog extends Animal{

...

    @Override

    public void animalSound(){

        System.out.println("Woof");

    }

}

Static Polymorphism Example

Method overloading ဟာ static polymorphism ရဲ့ ဉပမာ ဖြစ်တယ်။ An overridden method ကို compile လုပ်တဲ့အချိန်မှာ resolve လုပ်တယ်။


class DisplayOverloading

{

    public void disp(char c)

    {

         System.out.println(c);

    }

    public void disp(char c, int num)  

    {

         System.out.println(c + " "+num);

    }

}

public class ExampleOverloading

{

   public static void main(String args[])

   {

       DisplayOverloading obj = new DisplayOverloading();

       obj.disp('a');

       obj.disp('a',10);

   }

}

Dynamic Polymorphism Example

An overridden method ကို runtime မှာ resolve လုပ်တဲ့အတွက် Runtime Polymorphism လို့ခေါ်တယ်။ Dynamic Method Dispatch လို့လည်းခေါ်တယ်။ ဟာ static polymorphism ရဲ့ ဉပမာ ဖြစ်တယ်။


class Animal{

   public void animalSound(){

System.out.println("Default Sound");

   }

}

public class Dog extends Animal{


   public void animalSound(){

System.out.println("Woof");

   }

   public static void main(String args[]){

Animal obj = new Dog();

obj.animalSound();

   }

}

Abstract Class

​​Abstract ဆိုတဲ့ keyword အသုံးပြုပြီး Abstract Class ကို ဆောက်လို့ရတယ်။ Abstract class မှာ method တွေကို declare ပဲလုပ်ပြီး define မလုပ်ပဲထားတယ်။ Abstract class ကို ခေါ်သုံးတဲ့ class မှာ define လုပ်ရတယ်။ 


abstract public void playInstrument();

Abstract Class Example


//abstract class

abstract class AnimalAbstract{

   //abstract method

   public abstract void animalSound();

}


public class DogAbstract extends AnimalAbstract{


   public void animalSound(){

System.out.println("Woof");

   }

   public static void main(String args[]){

Animal obj = new Dog();

obj.animalSound();

   }

}

Interface

Interface ဆိုတဲ့ keyword အသုံးပြုပြီး Interface ကို ဆောက်လို့ရတယ်။ Abstract class မှာ method တွေကို declare ပဲလုပ်ပြီး define မလုပ်ပဲထားတယ်။ Abstract လိုပဲ Interface ကို ခေါ်သုံးတဲ့ class မှာ implement လုပ်ရတယ်။ Interface ကို full abstraction ရဖို့အတွက် အသုံးပြုတယ်။


interface MathematicianInterface{}

Interface Example


interface MathematicianInterface {

String expertiseInMath="hello";

void solveMathProb();

}


class Teacher2 {

   String designation = "Teacher2";

   String college = "SofteraComputer2";

   void does(){

System.out.println("Teaching");

   }

}


public class MathTeacherClass2 extends Teacher2 implements MathematicianInterface{


   String mainSubject = "Maths2";


   public void solveMathProb(){

    System.out.println("Printing from Interface.");

   }


   public static void main(String args[]){

      MathTeacherClass2 obj = new MathTeacherClass2();

      System.out.println(obj.college);

      System.out.println(obj.designation);

      System.out.println(obj.mainSubject);

      obj.does();

      //interface

      obj.solveMathProb();

   }

}


************************************






Comments

Popular posts from this blog

စာသင်ကြားရန် အသုံးပြုသည့် ဆော့ဖ်ဝဲများ

သင့်ရဲ့ ပထမဉီးဆုံးသော ကွန်ပျူတာ ပရိုဂရမ် (Hello, World!)