OPP - Object Oriented Programming


Standard for OPP

Java class සදහා නමක් යෙදීමේදී නම ආරම්භ කල යූත්තෙ latter,  “_” හෝ “$” මගින් වේ. තවද නමෙහි අර්ථ 2ක් යෙදෙන විට එහි මුල් අකුර simple කරා මැද නමෙහි මුල් අකුර කැපිටල් සිදු කරයි. මෙලෙස යෙදීම camel case ලෙස හදුන්වයි.

මෙමගින් class, method සහා variable වශයෙන් වෙන කර හඳුනාගත හැකිය.

Ex :-                                                                Ex :-

    public class _Java {                                            public class myJava {        
        int x = 10;                                                           int x = 10;

    }                                                                         }


Class 

Java OPP ලෙස වල class එකක් නිර්මාණය කිරීමේදී එහි class name එකට නමක් යෙදීමේදී එම නමෙහි මුල් අකුර capital අකුර ලෙස නිරූපණය කරයි.

Ex :-
    
    public class Java{
        int x = 10;
    }

Method

java Method නිර්මාණය කර එහි නමට පසුව වරහන් () තිබිය යුතුය. මෙය Parameter ලෙස හදුන්වයි

Ex :- 

        public void java (){
        int x = 10;
    }

Variable

java Variable නමක් සදහා variable name simple අකුරු මගින් නිරුපනය කරයි.

Ex:- 
    
     public class Java{
        String number = "10" ;
        String name =  "sachitha" ;
    }


Key Words

class, method and variables සදහා නමක් ලබා දීමේදී සෑම නමක් ම ලබා දිය නොහැක. එම නාමයන් java සදහා විශේෂ වූ නාමයන් වේ. එය keywords ලෙස නම් හදුන්වයි.



What is the  Object?

properties සහ behaviors ලෙස තියෙන දේවල් object ලෙස හැදින්වේ. 
What is the Class?

object එකක් සදහා නිර්මාණය කරන ආකෘතිය ට class ලෙස හැදින්වේ. 


Object Generating

    class Human {
        }
        Ex :-
                  Human name = new Human();
                
                       name.hands = 2;
                        name.legs = 2;
    
                        name.run();
                        name.jump();


Data Types

දත්ත ගබඩා කිරීමට බාවිතා කරන විවිද දත්ත වර්ගයන් data type ලෙස හැදින්වේ.
ප්‍රධාන වශයෙන් data types කොටස් 2 කි.
                                    1. Primitive Data Types
                                    2. Non - Primitive Data Types
        
    1.   Primitive Data Types
            primitive data වර්ග 8 කි. 

        
       
  2.     Non-Primitive Data Types
                String,
                Class
                Array, etc.

Access Modifiers

    class, method and variable සදහා ප්‍රවේශ වීමට ලබා දෙන ආරක්ෂාව වැඩිදියුණු කිරීමට 
    Access Modifiers බාවිතා කරයි.

                1. public            -    anyone can use
                2. protected       -    class / package / sub class
                3. private           -    class
                4. default           -    class / package

Main Method

    class එකක් තුල ඇති methods තිබේ නම්, එම methods run කිරීමට main method තිබිය යුතුය.

Parameter & Arguments

parameter එකක් තුල අපි ලබා දෙන data argument එක අනුව data input and output                        වීම සිදු කරයි. ඒ සදහා default return type(void) එක වෙනුවට data type එකක් වුවද බාවිතා        කල හැක.එවිට method එක තුල එම අගයන් "return" කල යුතුය.

    Ex :-                                                        Ex :-
            void drive(int x){                               void tree(int x, String y){
            }                                                          }
            drive(7);                                              tree(100, "mango"):
    Ex :-
            int sum(int x, int y, int z){
            return;
            }
            sum(07, 69, 99);

  Constructor

    class එකේ නමින්ම හැදෙන return type එකක් නැති method එකකි. constructor එකක් කෝල්     වෙන්නේ object එකක් run වෙන අවස්තවෙදිමයි. class එකක මුලින් run වෙන්න ඕන code,            codding කරන්නේ constructor එක තුලය. 

        

    Method Overloading

        එකම class එකක, එකම නමින් method  කිහිපයක් ඇති විට එහි parameter signature එකේ             arguments එක change කිරීමෙන් methods වෙන් වෙන්ව හදුන්වාදීම method overloading වේ.

    Inheritance

    එක (Super/parent) class එකක තියෙන දේවල් (object, methods, variable) තවත් (Sub/child)            class එකකට ලබා දීම inheritance ලෙස හැදින්වේ.

    Method Overriding

        super class එකකින් sub class  එකකකට inherit වෙලා ලැබෙන method එකක body (code in         method ) එක වෙනස් කිරීම Overriding ලෙස හැදින්වේ.


            Ex :-
                    class Monkey{
                        void climb( ){
                            System.out.println(" Usung Body");
                        }
                    }
                    class Man extend Monkey{
                        void climb( ){
                            System.out.println(" Using Ladder");
                        }
                    } 
    
    නමුත්,
    Overriding වලදී object  එකක් හදල method එකක් call කරාම default run වෙන්නේ child / sub     ගේ method එක වේ.

          Ex :-
                class Father{
                    void son( ){
                        System.out.println(" Father's Son");
                    }
                }
                class Mother extend Father{                                                                
                    void son( ){                                                                                         OUTPUT:
                        System.out.println(" Mother's Son");                                                Mother's Son
                    }
                }
                class Test{
                    public static void main(String[] args){
                        Mother m = new Mother( );
                        m.son();
                    }
                }

    Super
        
        sub class එකේ ඉදන් super class එකේ (constructer, method, variable) වලට call කිරීම.


    Casting

        01. Up Casting
            super class  එකේ variable(reference) එකට sub class එකේ object එකක් assign කිරීම                     upcasting වේ. ( A is parent class and B is child class)

                                                    A a = new B();

        02. Down Casting
            super class variable එකට දාපු sub class object එක නැවත sub class variable එකක් කිරීම.

                                                    B b = (B) a;
    Polymorphism
        
        before use polymorphism firstly you know,
                                                                                1. Inheritance
                                                                                2. Overriding
                                                                                3. Upcasting
        
        super class එකක method එකක් sub class එකකදී override කල පසු upcasting මගින් super            class  reference එක true sub class method run කිරීම.

    Abstraction
    
        meaningless class & methods තියා ගන්න පුළුවන් Abstraction keyword එක use කරලා.
                                                    
                                                    void parking ( ) { }                     
                                               abstraction void parking ( ) ;       

       abstraction class එකක් තුල පමණයි abstraction method එකක් තියා ගන්න පුළුවන් සහ                normal method use කල හැක.

                                                    abstraction class Vehicle( ) {                     
                                                         abstraction void parking ( );
                                                         void drive ( ) { }
                                               }
                                                    class Car extends Vehicle ( ) { }
                                                    
        abstraction වලින් object create කල නොහැක. නමුත් upcasting මගින් object create කල                හැක.

                                                     Vehicle vehicle = new Vehicle ( );    ❌
                                                     Vehicle vehicle = new Car ( );            


    Interface

        inheritance මගින් multiple inheritance කල නොහැක. එම කාර්යය Interface මගින් සිදු කල
        හැක. 
                                            Object Generate
                                            
                                                    C name = new B( );      ✔
                                                    D name = new B( );      
                                                    D name = new D( );    


    Encapsulation

        data protect කිරීමට use කිරීමට ගනු ලබන concept එකකි. (data hiding) 
                                                    private    id;
                                                    private    name;
                                                    private    age;

        private මගින් එම class එකට පමණක් data access කල හැක.

                                                    public    int    getId( ){
                                                        return id;
                                                    }
                                                    public    void    setId( int id ){
                                                        this.id = id;
                                                    }
        encapsulation මගින් data privacy එක protect කර ගෙන get සහ set මගින් methods use                     කිරීම.

Comments