As Super Class objects cannot be instantiated in the main function abstract keyword is specified before the class name.But what difference does it make if an abstract keyword is used before the SuperClass over-riding method or not used. Can someone explain it please?
Here is the below example.Please check the commented part.
abstract class Figure
{
int dim1;
int dim2;
Figure()
{
dim1=-1;
dim2=-1;
}
Figure(int p,int q)
{
dim1=p;
dim2=q;
}
abstract void Area() //This line is working without abstract for me.
{
System.out.println("The area is undefined.");
}
}
class Rectangle extends Figure
{
int vol;
Rectangle()
{
super();
}
Rectangle(int p,int q)
{
super(p,q);
}
void Area()
{
vol=dim1*dim2;
System.out.println("The area of the rectangle is: "+vol);
}
}
class Triangle extends Figure
{
int vol;
Triangle()
{
super();
}
Triangle(int p,int q)
{
super(p,q);
}
void Area()
{
vol=dim1*dim2/2;
System.out.println("The area of the rectangle is: "+vol);
}
}
public class Area
{
public static void main(String[] args)
{
Rectangle r=new Rectangle(10,20);
Triangle t=new Triangle(6,10);
Figure fref;
fref=r;
r.Area();
fref=t;
t.Area();
}
}
abstractmeans that it’s body will be defined in it’s derived class.if you try to define a body for it, it’ll be compiler time error.
So, as a rule of thumb: