අපි මුලින්ම බලමු 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 :
අපි 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) බාගත කරගැනීම සඳහා පහත ලින්ක් එක භාවිතා කරන්න.








