Friday, January 29, 2021

Java Programming sinhala tutorial - Inheritance - Part 26

 Inheritance

    Java වල OOP concept 4 ක් පවතිනවා. ඒ තමයි,
  1. Inheritance
  2. Encapsulation
  3. Polymorphism
  4. Abstraction
    අද අපි inheritance ගැන දැන ගමු. යම් කිසි කෙනෙකු විසින් නිර්මාණය කල class එකක features (fields and methods) වෙනත් නිර්මාණයක් සදහා භාවිතා කිරීම inheritance යනුවෙන් සරලවම අපිට හදුනා ගන්න පුළුවන්. මේ සදහා extends keyword එක පාවිච්චි කරන්න පුළුවන්.
    කලින් නිර්මාණය කර තිබු හා එහි features inherit කිරීමට ඉඩ දෙන class එක super class (base class/ parent class) එක ලෙසද  එම  features inherit කරගෙන අලුතින් නිර්මාණය කරන class එක sub class (derived class/ extended class/ child class) එක ලෙසද හදුන්වයි.
    Inheritance සංකල්පය reusability සදහා උපකාරී වේ.


    අපි sub class එකට super class එක extend කල විට super class එකෙහි සියලුම private නොකළ methods හා  variables sub class object එකේ memory එකට ලබා ගන්නවා. ඒ නිසා sub class එක මගින් super class එකේ features අපිට ලබා ගන්න පුළුවන්. Sub class reference එකට super class එක ගැන knowledge එකක් පවතින නිසා sub class reference එක මගින් super class එකේ methods හා  variables access කරන්න පුළුවන්.

    මේ inheritance ක්‍රියාවලියේදී subclass object එක පමණයි create වෙන්නේ. Inheritance ක්‍රියාවලියේදී super class එකේ byte code එක sub class file එකෙහි නොපවතී. Sub class එක හා super class එක අතර logical link එකක් පමණක් පවතී. Sub class object එකක් නිර්මාණයේදී පළමුව super class එක template object එකක් ලෙස load වේ. ඊට පසු sub class එක  template object එකක් ලෙස load වේ. ඊට පසු sub class object එක නිර්මාණයවේ. එම නිසා sub class එකක් වෙනත් computer එකක run කරවීමට super class එකද එම computer එකට ඇතුලත් කල යුතු වේ.
    
    

    Sub class එක නිර්මාණයේදී super class එකෙහි methods හා variables ද sub class object එකේ නිර්මාණය වන බව ඔයාලා දැන් දන්නවනේ. කලින් ලිපියෙන් මං object  initializer ගැන විස්තර කලානේ. object  initializer variables initialize කිරීමේදී පළමුව super class එකෙහි variables ද ඉන් පසු sub class එකෙහි variables ද initialize කරයි. Sub class එක run වන විට super class එකෙහි කුමන constructor එක run විය යුතු දැයි ඇතුලත් කර නොමැති නම් compiler විසින් sub class එකේ constructor එක තුල super(); command එක ඇතුලත් කරයි. එවිට object initializer පළමුව super class එකෙහි default constructor එක run කර ඊට පසු sub class එකෙහි constructor එක run කරයි.
    අපට super class එකෙහි parameter සහිත වෙනත් constructor එක run කළ යුතු නම් ඊට අදාල  parameter සහිත super command එක ඇතුලත් කළ යුතු වේ.



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

- චමෝදි -

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 ගැන විස්තර ඉගෙන ගනිමු.

- චමෝදි -

Saturday, January 9, 2021

Java Programming sinhala tutorial - Class & Object - Part 24

Class & Object (Part 1)

    Java language එක කියන්නේ Object Oriented Programming language එකක්. ඒ කියන්නේ object කියන සංකල්පය පදනම් කරගෙන ගොඩනගපු language එකක්. ඒ අනුව මේ language එකේ මුලික පදනම class හා object වෙනවා.

    අපි මුලින්ම බලමු මොකද්ද මේ class එකක් කියන්නේ කියලා. Class එකක් කියලා සරලවම කියන්නේ object එකක තිබිය යුතු කරුණු සදහන් කර යම් පුද්ගලයෙකු නිර්මාණය කරන ලද blueprint නැතහොත් prototype එකක්. එහි එම class type එකෙන් නිර්මාණය කරන සියලුම object වල පොදුවේ තිබිය යුතු properties හා methods set එකක් අඩංගු වෙනවා. ඒ අනුව object එකක් කියන්නේ properties හා methods ගොඩක එකතුවක් කියලා අපිට සරලවම හදුනා ගන්න පුළුවන්.

    Object එකක් initialize කරන්න constructors පාවිච්චි කරන්න පුළුවන්. Class එක නිර්මාණය කරන විට constructor එකක්වත් නිර්මාණය කරලා නැතිනම් complier විසින් argument නොමැති constructor (default constructor) එකක් නිර්මාණය කරයි. මෙම default constructor එක එම class එකට inherit කර ඇති parent class එකෙහි argument නොමැති constructor එකට call කරයි. ඊට හේතුව වනුයේ super(); statement එක එම constructor එහෙහි අඩංගු වීමයි. එම class එකට parent class එකක් නොමැති නම් Object class constructor එකට call කරයි. ඊට හේතුව වනුයේ සැම class එකකම parent class එක සෘජුව හෝ වක්‍රව Object class එක වීමයි. Constructors ගැන වැඩි විස්තර ඉදිරි ලිපි වලින් මම කියන්නම්. 

    Class එකක හෝ එහි objects වල ඇති variable වලින් එහි state එකද method වලින් එහි behavior එකද පෙන්නුම් කරනවා. 

    අපි දැන් බලමු කොහොමද object එකක් initiate/ declare කරන්නේ කියලා.






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

- චමෝදි -

Friday, January 1, 2021

Blender Sinhala Tutorial - Doughnut - Part2

 අපි එහෙනම් ඩෝනට් එකේ ඉතිරි ටික හදාගමු.


ඉස්සෙල්ලම edit mode එකට යන්න. 










Edit mode එකට ගියාට පස්සේ ඩෝනට් එක මෙන්න මේ විදියට තමයි පෙන්නන්නේ. හොඳට මතක තියාගන්න. මේ mode එකේදී light සහ camera පෙන්නන්නේ නැහැ









ඊට පස්සේ එක පාරක්  left click කර select වෙලා තියෙන nodes deselect කර ගන්න. ඊට පස්සේ right click කර එක node  එකක් select කර ගන්න. ඊට පස්සේ  ctrl+G  press  කරන්න. එතකොට ඔයාලට බලාගන්න පුළුවන් මේ node එක mouse එකේ movement එකත් එක්ක එහෙ මෙහෙ යනවා.

















ඩෝනට් එකක් කියන්නේ හරියටම රවුම් හැඩයක් තියෙන එකක් නෙමෙයි. ඉතින් දැන් මට ඕනේ මේ shape එක ටිකක් අක්‍රමවත් කර ගන්න. හැබැයි ඉතින් මෙහෙම node එකින් එක ඇද ඇද ඒක කරන්න නම් අමාරුයි. ඒ නිසා මම proportional editing පාවිච්චි කරනවා. 















දැන් එක node එකක් select කරලා move කරද්දී ඒ node එකට සම්බන්ධ සියලුම node move වෙනවා බලාගන්න පුළුවන්. 
















හරි දැන් එහෙම කරලා ඩෝනට් එකේ shape එක ගන්න ඕනේ. මේ තියෙන්නේ මන් එහෙම හදාගත්තු ඩෝනට් එක.










තාම අපේ ඩෝනට් එක තියෙන්නේ කොටු කොටු හැඩයේ. ඉතින් මට ඕනේ smooth edges තියෙන ඩෝනට් එකක්. එකට මම කරන්නේ වම පැත්තේ තියෙන මෙනු එකින් ස්මූත් කියන option එක තෝරන  එක.


එතකොට මෙන්න මේ වගේ ඩෝනට් එක බලාගන්න පුළුවන්. 


තවත් ටිකක් ස්මූත් උනා නම් හොදයි වගේ මට හිතෙනවා. ඒ නිසා මම subdivision modifier එක use කරනවා.






 Modifier එක apply කරන්න කලින් සහ පස්සේ පෙනුම තමයි මේ පහල ෆොටෝ වලින් පෙන්නන්නේ.

Before adding modifier



After adding modifier



හදල අවසාන උනු ඩෝනට් එක මෙන්න මේ වගේ තමයි පෙන්නේ.

ඊලග පොස්ට් එකින් මම ගේන්නම් කොහොමද ඩෝනට් එක අයිසින් කරගන්නේ කියල.

~Hasini Senanayaka~