Now here is the java program:
class Figure{
double dim1;
double dim2;
Figure(double a,double b){
dim1=a;
dim2=b;
}
double area(){
System.out.println("Area is undefined.");
return 0;
}
}
class Rectangle extends Figure{
Rectangle(double a,double b){
super(a,b);
}
double area(){
System.out.println("Inside Rectangle Area.");
return dim1*dim2;
}
}
class FindArea{
public static void main(String args[]){
Figure f=new Figure(10,10);
Rectangle r=new Rectangle(9,5);
Figure figref;
figref=r;
System.out.println("The Area is "+figref.area());
figref=f;
System.out.println("The Area is "+figref.area());
}
}
And here is the output generated by the program:
Inside Rectangle Area.
The Area is 45.0
Area is undefined.
The Area is 0.0
But this is the output that I think is appropriate:
The Area is
Inside Rectangle Area.
45.0
The Area is
Area is undefined.
0.0
Now my reason to think that this is the right output is that as you can see the statement "The Area is " is called to print before the area method is even called so why is the statement "Inside Rectangle Area" coming at the beginning even though the area method is not the first thing to be called. Now I have tried this program and yes my assumed output is incorrect but I want to know why is it incorrect because there is nothing about this mentioned in the book and currently its summer vacation so I can't ask any teacher either so please help.
class Figure{
double dim1;
double dim2;
Figure(double a,double b){
dim1=a;
dim2=b;
}
double area(){
System.out.println("Area is undefined.");
return 0;
}
}
class Rectangle extends Figure{
Rectangle(double a,double b){
super(a,b);
}
double area(){
System.out.println("Inside Rectangle Area.");
return dim1*dim2;
}
}
class FindArea{
public static void main(String args[]){
Figure f=new Figure(10,10);
Rectangle r=new Rectangle(9,5);
Figure figref;
figref=r;
System.out.println("The Area is "+figref.area());
figref=f;
System.out.println("The Area is "+figref.area());
}
}
And here is the output generated by the program:
Inside Rectangle Area.
The Area is 45.0
Area is undefined.
The Area is 0.0
But this is the output that I think is appropriate:
The Area is
Inside Rectangle Area.
45.0
The Area is
Area is undefined.
0.0
Now my reason to think that this is the right output is that as you can see the statement "The Area is " is called to print before the area method is even called so why is the statement "Inside Rectangle Area" coming at the beginning even though the area method is not the first thing to be called. Now I have tried this program and yes my assumed output is incorrect but I want to know why is it incorrect because there is nothing about this mentioned in the book and currently its summer vacation so I can't ask any teacher either so please help.