Saturday, July 10, 2010

ජාවා තුල Abstract Classes යොදාගන්නා අයුරු

වස්තු පාදක(OO) ජාවා වැඩසටහන් ලිවීමේදී බහුල වශයෙන් යෙදෙන සංකල්පයක් තමයි abstraction කියන්නේ මේ සඳහා ජාවා පරිගණක භාෂාව තුල පහසුකම් සපයාතිබෙනවා. 

අපි මුලින්ම බලමු abstract class එකක ලක්ෂණ මොනවාද කියල.
  • Abstract class වලින් කිසිවිටෙකත් objects සෑදිය නොහැක.

  • Abstract class එකක් extend කර(inherit කර) පසුව සාදනු ලබන class වලට හොඳ පදනමක් සැපයීම.

  • Generic (පොදු) ස්වභාවයකින් යුතුවීම.  එනම් abstract class එකෙහි child class වලට පොදු ලක්ෂණ එතුල කැටිවී තිබීම(උදාහරණය මගින් විස්තර කර ඇත.)

  • Parent class එකෙහි Abstract methods ඇත්න්නම් එහි sub class නොහොත් child classes වලදී එම abstract methods අනිවාර්‍යයෙන්ම override කල යුතුය(override කරනවා යන්නෙහි සරල අදහස නම් අදාල abstract method එකෙහි body එක implement කල යුතු බවයි) 
දැන් අපි උදාහරණය බලමු
class : AbstractDemo.java
/**   
*class  : AbstractDemo   
*Author : Kanishka Dilshan   
*Purpose: Describe abstract concept in Java 
*Blog   : http://javaxclass.blogspot.com   
*/   

abstract class Shape{
 protected int color;
 protected Point pos; //position
 
 public void setColor(int color){
  this.color=color;
 }
 
 public int getColor(){
  return color;
 }
 
 public void setPos(Point p){
  pos=p;
 }
 
 public Point getPos(){
  return pos;
 }
 
 abstract public float getArea();
  /* This method is defined as abstract since
     it cannot be implemented since Shape is a  
     common class(abstract class) */
}

class Rectangle extends Shape {
 /*since this clsaa inherits Shape class
  *the getArea() method must be overrided
  */
  //thease are properties specific to a Rectangle
  private float width,height;
  
  public Rectangle(float w,float h,Point p,int col){
  super.color=col;
  super.pos=p;
  this.width=w;
  this.height=h;
  }
  
  public float getArea(){
  return (width*height);
  } 
}

class Circle extends Shape {
 /*since this clsaa inherits Shape class
  *the getArea() method must be overrided
  */
  private float radius;
  
  public Circle(float rad,Point p,int col){
  super.color=col;
  super.pos=p;  
  this.radius=rad;
  }
  
  public float getArea(){
  return (float)(Math.PI*Math.pow(radius,2));
  }   
}

class Point{
 //class definition for represent a Point
 protected int xPos;
 protected int yPos;
 public Point(int x,int y){
  xPos=x;
  yPos=y;
 }
 
 public int getX(){
  return xPos;
 } 
 
 public int getY(){
  return yPos;
 }
}


public class AbstractDemo{
 //main method
 public static void main(String args[]){
  Rectangle rect1=new Rectangle(12.0f,7.5f,new Point(10,-3),255);
  Circle c1=new Circle(4.3f,new Point(3,8),100);
  
  //let's prnint object details for rect1
  println("rect1 belongs to    : "+ rect1.getClass().toString());
  println("colour val of rect1 : "+ rect1.getColor());
  println("x position of rect1 : "+ rect1.getPos().getX());
  println("y position of rect1 : "+ rect1.getPos().getY());
  println("area of rect1       : "+ rect1.getArea() );
  
  println("-----------------------------------------");
  //let's prnint object details for c1
  println("c1 belongs to       : "+ c1.getClass().toString());
  println("colour val of c1    : "+ c1.getColor());
  println("x position of c1    : "+ c1.getPos().getX());
  println("y position of c1    : "+ c1.getPos().getY());
  println("area of c1          : "+ c1.getArea() );
 }
 
 public static void println(Object o){
  System.out.println(o);
 }

}
output :
අපි දැන් ඉහත code එක විස්තරාත්මකව අධ්‍යයනය කරමු
අපි abstract class එක ලෙස මෙහිදී යොදාගෙන තිබෙන්නේ Shape නම් class එකයි. එයට හේතුව වනුයේ Circle සහ Rectangle යන class දෙකටම පොදු ලක්ෂණ Shape තුල අන්තර්ගත වන නිසයි. 

Line 28
abstract public float getArea();
මෙහි getArea() නම් method එක abstract ලෙස සටහන් කර ඇත්තේ එය Shape හි sub classes වලට (specific) අනන්‍ය නිසා Shape class එක තුල implement කල නොහැකි නිසාය.

Line 48 - 50
public float getArea(){
  return (width*height);
  }
මෙහිදී Rectangle class එකෙහි super class එක වන Shape class එකෙහි ඇති abstract method එක implement කර ඇත. super class එකෙහි abstract method යම් ප්‍රමාණයක් ඇත්නම් ඒ සියල්ලම එහි subclasses වලදී implement කිරීම අනිවාර්‍ය වේ.
මෙම පාඩමෙන් අප ලබාගත් දැණුම මීලඟ පාඩම වන interfaces පාඩමේදී අත්‍යාවශ්‍ය වන නිසා මෙය හොඳින් අධ්‍යයනය කරන්න.

මෙහි ගැටලු සහගත තැනක් වේ නම් email එකක් මගින් දන්වන්න නැත්නම් comment එකක් ලෙස හෝ ඉදිරිපත් කරන්න.

පාඩමේ භාවිතා කර ඇති මූලාශ්‍ර කේත(source codes) බාගත කරගැනීම සඳහා පහත ලින්ක් එක භාවිතා කරන්න.

17 comments:

  1. වටින පාඩම් මාලාවක් දිගටම කරගෙන යන්න යාලුවා

    ReplyDelete
  2. ස්තුතියි අයියා.

    ReplyDelete
  3. යමක් ඉගෙනගතා... thanx

    ReplyDelete
  4. exception handling
    Threads
    jdbc & odbc
    file hanling
    we want to know..

    ReplyDelete
  5. ok Lahiru,
    I'll discuss on those areas in future lessons.
    Thank you.

    ReplyDelete
  6. mahi Abstract class eke variable declare kirimedi Protected bavitha kale ai? wenath data access control kramayak bavitha kala nohekida?

    ReplyDelete
  7. @Anonymous
    භාවිතා කල හැකියි.. නමුත් මෙහිදී වඩාත් සුදුසු protected යන access control එකයි. protected නිසා අදාල property එක access කල හැක්කේ අදාල class එකට සහ එහි child class වලට පමණි.

    ReplyDelete
  8. protected int color;

    public void setColor(int color){
    this.color=color;
    }

    abstract class eka thula
    public int color;
    lesa color property eka declare nokota ihatha paridi constructor eka call karanne ai?

    ReplyDelete
  9. එසේ කල හැකි වුවද එය හොඳ programming practice එකක් නොවේ.. variable සඳහා එලෙස public access ලබාදීමට ගියවිට encapsulation ගුණාංගය අප ලියන වැඩසටහනෙන් ඉවත්වී යයි. එය ගැටලු රාශියක ආරම්භයකි.

    ඉහත setColor(int color) යනු constructor එක නොවේ එය member function එකකි.

    ReplyDelete
  10. Thanx a lot. meka java sinhalen egena ganna ayata sirama sira site 1kak. patta ☺☺☺

    ReplyDelete
  11. wade ela kiri... digatama karagena yanna subha pathanawa.. PIN SIDDA WENAWA.....

    ReplyDelete
  12. well done brother!! your lecture style is excellent.we can study easily alone with your lessons. i wish you brilliant future

    ReplyDelete
  13. බොහොම ස්තුතියි ලොක්කා,, ජය වේවා,, !!

    ReplyDelete
  14. Bro am new to this blog and found your work is excellent.

    This is my question,

    We use abstract method inside of abstract class to declare method in more generic form.

    Ex:
    abstract public float getArea();

    }

    And the method is override when it implement in an object.

    But my question is Why it use abstraction method and override process rather than directly declare that method in real object class.

    For Ex: we can declare getArea method inside of Rectangle class directly when we creating object with out creating that method as an abstract method of another super generic class.

    I feel most the time that this abstract is making the programming more complex. If you can clarify my problem it will be grate help.

    Cheers

    ReplyDelete
  15. @Shanika,
    abstraction assures that child classes override parent's abstract methods. this is essential in some scenarios.
    For example if you want to maintain a proper inheritance hierarchy it is not a good practice to put methods here and there.

    ReplyDelete