Wednesday, May 14, 2014

OOP Part 2 INHERITANCE


මුලින්‍‍ලුනේ  sorry ‍කිටන්‍ ‍ගන්‍‍නම්‍ post  කක්‍ leat  නාමොගොඩාක්‍ ‍වැපිහිවෙදියෙ 
 ‍හොයිදැන්‍ ‍හෙනම්‍ ‍ටං‍‍මුපායාකියන්‍‍යන්‍‍නෙ java  වත්‍ ‍වැගත්‍ ‍කොසක්‍ ‍ගැ. ‍යි java   inheritance .

යම්  class ‍  objects ‍වෙනත්‍ class  සා objects  ගුනාන්තුලත්‍‍සිදු ක්රියායි inheritance  කිකියන්‍‍නෙ . ‍නිසාමෙ class ‍දෙපොදුවූගුනාන්ග  තිබෙ. ‍
 දාලක්‍‍යත්  class ‍ child   class ‍ලෙසත්‍ ‍දාලක්‍‍මුලින්‍ ‍තිබු class  parent  හෙවත්‍ super   class ‍ලෙදුන්‍‍යි.

මෙ ක්රියාහාජාවාහිති extend  නම්‍ key   word ‍බාවිතානුයි . ‍යාවැහෙති software   engineering  හිබාවිතා software   reusability ‍සන්‍‍කල්‍‍මෙකොසේදීහොදින්‍ ‍බාවිතා child   class ‍කේ parent   class ‍කේනොමැති attributes  behaviour  තිබෙන්‍‍පුලුවන්‍.
‍‍මෙහිමූලිරුනුකිහියක්‍‍තිනම්

1.      new   class ‍සෑකක්‍‍ extend  යුතු .
2.      extend  කිරීවිශෙයෙන්‍ ‍‍හන්‍ ‍නොවි object  class  inherit  වීසිදුවේ
3.      inherit ‍වීමෙදීකිසිවිකත්‍ constructor  යෝගීවීසිදුනොවේ.. ‍ජාවාහිසෑ  class ‍තිබිහැක්‍‍කේ parent   class ‍කක්‍ ‍නි javaa   multiple  inheritance ‍හානොදක්‍‍යි.
දැන්‍ ‍පිමු inheritance  පිලි java   code ‍කින්‍‍ම(‍ම‍ම ‍මේ ‍ස‍ද‍හා ‍බා‍වි‍තා ‍ක‍රන්‍‍නේ ‍සා‍මාන්‍ය ‍සි‍ලින්‍‍ඩ‍ර‍යක්‍ස‍හ ‍වර්‍න ‍ගැන්‍‍වූ ‍සි‍ලින්‍‍ඩ‍ර‍යක්‍ ‍ය‍න ‍උ‍දා‍හ‍ර‍න‍ය‍යි)


Code:-01

public class silinder {
    private int rValue;
    private int height;

    public silinder(int r,int h) {
        rValue=r;
        height=h;
    }
    public void setr(int r){
        rValue=r;
    }
    public void seth(int h){
        height=h;
    }
    public double getVolume(){
        return(Math.PI*(rValue*rValue)*height);
    }
    public void print() {
        // TODO Auto-generated method stub
        System.out.println("r value ="+rValue);
        System.out.println("height  ="+height);
        System.out.println("Volume  ="+getVolume());
    }

}


colour silinder class



public class colourSilinder extends silinder {
    private String colour;

    public colourSilinder(int r,int h,String c) {
        super(r, h);
        colour=c;
    }
    public void setColour(String c){
        colour=c;
    }
    public void print(){
        super.print();
        System.out.println("Colour  ="+colour);
    }

}




main class


public class inheritance {

    public static void main(String[] args) {
        silinder s=new silinder(1, 1);
        colourSilinder cs=new colourSilinder(2, 5, "red");
      
        s.print();
        System.out.println("_____________________________");
        cs.print();
      
      
        System.out.println("_____________________________");
        System.out.println("Making changes silinder Objects");
        System.out.println("New information");
      
        s.seth(2);
        cs.setColour("Green");
        cs.setr(2);//this method inherit super class
      
        s.print();
        System.out.println("_____________________________");
        cs.print();
    }

}


‍හො‍ද‍යි ‍දැන්‍ ‍ඔ‍යා‍ල‍ට ‍හො‍ද‍ට ‍වැ‍ට ‍හෙ‍න‍ව ‍ඇ‍ති inheritance  ‍කි‍යන්‍‍නෙ ‍මො‍කද්‍‍ද ‍කි‍ය‍ල ‍මේ‍ගැ‍න ‍ත‍වත්‍ ‍අද්‍ය‍න‍ය ‍ක‍රන්‍‍න ‍‍මූ‍ලි‍ක ‍සන්‍‍කල්‍‍ප‍ය ‍ත‍ම‍යි ‍මේ. ‍
එ‍හෙ‍නම්‍ ‍යා‍ලු‍ව‍නේ ‍අ‍ද‍ට ‍පා‍ඩ‍ම ‍ඉ‍ව‍ර‍යි ‍ත‍වත්‍ ‍ද‍ව‍ස‍ක ‍හ‍මු‍වෙ‍මු.. 
                                                                               
By Manoj Priyankara