Friday, January 15, 2021

Java Programming sinhala tutorial - Class & Object - Part 25

Class & Object (Part 2)

    කලින් ලිපියෙන් අපි class හා object ගැන මුලික කරුණු දැන ගත්තනේ. අද අපි ඒ ගැන වැඩිපුර විස්තර ටිකක් දැනගමු.

Memory Representation of an Object

    අපි යම් source code එකක් execute කරන විට, hard disk එකේ පවතින වෙනත් object එකක් (ex :- Box object ) නිර්මාණය කිරීමට අවශ්‍ය යැයි command එක run වුවහොත්, පළමුව එම object එකේ .class file එක RAM එක තුල allocate වේ. ඉන් පසු RAM එකෙහි heap එක තුල object එක allocate වේ.



"this" keyword

    "this" යනු keyword එකක් හා predefine variable එකකි. එය තුල current variable එකෙහි address එක store වී ඇත. Method එකක් ක්‍රියා කරන විට  object එකේ හා variable stack එකේ එකම නමින් variable පවතී නම් ඒවා වෙන් කර හදුනාගන්න මෙමගින් පුළුවන්.


    මෙහිදී "this" keyword එක භාවිතා කිරීම නිසා variable stack එකෙහි ඇති variable නොසලකා object එකෙහි ඇති variable භාවිතා කර ඇත.

Default Value

    Object එකක් නිර්මාණය කරද්දීම එහි attributes initialize වේ. එලෙස object එකෙහි attributes සදහා store වන්නේ එම attribute එකෙහි data type එකට අදාල default value එක වේ.


Default Values :-

  • byte - 0
  • short - 0
  • int -  0
  • long - 0
  • float - 0.0
  • double - 0.0
  • char - null char
  • boolean - false
  • Reference Type, class type - null

Declaration Value

    Object එකෙහි attribute/ variable නිර්මාණය කරන විටම ඒවා initialize කල හැක. එවිට එම value, declaration value නම් වේ.


Constructor

    Object එකක් නිර්මාණය කරද්දීම යම් දෙයක් සිදු විය යුතු නම් එය එම class file එක තුල constructor එකක් තුල නිර්මාණය කල හැක. JVM එක මගින් automatically, object එකක් නිර්මාණය කරන විටම constructor එක execute කරයි. ඒ සදහා method call කල යුතු නොවේ. constructor එකෙහි නම class name එකට සමාන වන අතර එයට return type එකක් නොමැත. constructor එක ආරම්භයේදී return type එකක්/ void යෙදුවහොත් මෙය normal method එකක් බවට පත් වේ. constructor එකකට call කර ක්‍රියා කල නොහැකි අතර JVM එකට පමණක් මෙය ක්‍රියාකරවිය හැක.


Parameterized Constructor vs Default Constructor

    Constructor එකෙහි parameters නොමැති නම් ඒවා default constructor නම් වන අතර constructor එකෙහි parameters පවතී නම් ඒවා parameterized constructor නම් වේ. Parameterized constructor ක්‍රියා කරවීමට object එක නිර්මාණයේදී arguments ලබා දිය යුතු වේ.
    Object එකක් නිර්මාණයේදී constructor එකක් අත්‍යවශ්‍ය වේ. Constructor එකක් class file එකේ නොමැති නම් JVM එක default constructor එකක් නිර්මාණය කරයි. 


Constructor Overloading

    Constructor එකකට වඩා වැඩි ප්‍රමාණයක් class file එකෙහි නිර්මාණය කර ඇති විට, object එක නිර්මාණයේදී දෙනු ලබන arguments අනුව constructor  එක call වේ.

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;
    }

    Box(int l) {
        length = l;
        width = l;
        height = l;
    }

    void setSize(int l, int w, int h) {
        length = l;
        width = w;
        height = h;
    }

    void printVolume() {
        int volume;
        volume = length * width * height;
        System.out.println("Volume of the box is : " + volume);
    }

    void printArea() {
        int area;
        area = 2 * length * width + 2 * length * height + 2 * width * height;
        System.out.println("Area   of the box is : " + area);
    }
}

class Demo {
    public static void main(String args[]) {
        Box b1 = new Box();
        b1.printVolume();

        Box b2 = new Box(12);
        b2.printVolume();

        Box b3 = new Box(12, 5, 3);
        b3.printVolume();
    }
}

Output :- 

Volume of the box is : 1
Volume of the box is : 1728
Volume of the box is : 180 

Calling Constructor using "this(xxx)"

    Constructor එකකින් එම class file එකේම වෙනත් constructor එකකට call කිරීම සදහා this(xxx); ලෙස command කරයි ( මෙහි xxx යනු parameter වේ. ). එනම් constructor එකකට වෙනත් constructor එකක් reuse කිරීමට මෙමගින් හැකි වේ.



Static Variable/ Class Variable/ Template Variable

    Object එකේ variable/ attribute එකක් static නම් එම variable එක template එකෙහි නිර්මාණය වේ. Static variable සදහා value assign කල විට එම value object එකෙහි store නොවන අතර එම value template එකෙහි store වේ. එම නිසා සැම object එකකටම පොදුවේ එම static variable එක භාවිතා වේ.


    Static variable template එකෙහි නිර්මාණය වන නිසා object එක නිර්මාණය නොකර ද main method එක මගින් static variable ලබාගත හැක. එවිට එම static variable, class name එක මගින් identify කර ගත හැක.


Static Method/ Class Method/ Template Method

    Static variable මෙන්ම static method ද  template එකෙහි නිර්මාණය වන අතර object එක නිර්මාණය නොකර ද main method එක මගින් static method call කල හැක. එවිට එම static method, class name එක මගින් identify කර ගත හැක.


Static Block

    Static block class file එකෙහි ලිවිය හැකි code block එකකි. Java class එක hard disk සිට RAM එකට load වන හැම විටකදීම automatically මෙය run වේ.


Instance Block

    Static block හි ක්‍රියාකාරිත්වයට සමාන ක්‍රියාවක් instance block මගින් සිදු කරයි. එනම් object එකක් නිර්මාණය කරන සැම අවස්ථාවකදීම instance block එක automatically run වේ.


Template Object, Static Initializer & Object Initializer

    Java class file එකක් hard disk එකේ සිට RAM එකේ JVM එක වෙත class loader software එක මගින් ගෙන එනු ලබයි. ඉන් පසු Class file එක මගින් නිර්මාණය කරන object එක තුල එය store වේ. Main method එහි එකක් පවතී නම් එය object එක තුලම වෙනත් තැනක store වේ. මෙම object එක template object එක ලෙස හදුන්වයි. 
    Main method එක run වන විට වෙනත් java class එකක් අවශ්‍ය නම් එය පළමු java class file එක පැවති folder එකෙහි හෝ JVM tool kit එකෙහි පවතී දැයි බලයි. මෙලෙස RAM එක වෙත එනු ලබන java class, Class file එක මගින් නිර්මාණය කරන object  තුල store වේ.


Static Initializer :-
Template object එක නිර්මාණයේදී static variable, template එකෙහි store වේ. මෙම variable එහි data type එකට අදාල default value එකෙන් පළමුව initialize වේ. Declaration value හෝ static block එක මගින් එම static variable declare කර ඇත්නම් variable එමගින් initialize වේ.

Object Initializer :-
මෙමගින් object එකෙහි attributes initialize කරනු ලබයි. Object නිර්මාණය වන විටම attributes ඒවායේ data type එකට අදාල default value එකෙන් initialize වේ. Declaration value හෝ instance block හෝ constructor එක මගින් එම attributes declare කර ඇත්නම් attributes එමගින් initialize වේ.

Declaration value හා  instance block පවතී නම් ඒවා constructor එක තුලට ඇතුළු කරගෙන object එක run වේ.



    අපි ඊලග ලිපියෙන් Java OOP concept ගැන විස්තර ඉගෙන ගනිමු.

- චමෝදි -

0 comments:

Post a Comment