Write a parent class Rectangle in Java with an area calculation method in dynamic binding and another area calculation method in static binding. Write a child class Square with an area calculation method that overrides the Rectangle class’s area method. ( in Java use static or final for static binding.) Write a main program that calls both the dynamically bound method and the statically bound method a large number of times (say 1000 times), timing the calls to static binding method as well as to the dynamic binding method respectively. Compare the results and explain the difference.
Java code for given problem is copied below with relevant comments. It creates two classes, Rectangle and Square (latter extends Rectangle class) and executes their area method under Main class large number of times.
square object we're creating under main of Square class is referenced by superclass, i.e., Rectangle and as such area_dynamic method is dynamically linked at run time while area_static is linked during compile time. Finally the execution timing is displayed as milliseconds and is timed using System.nanoTime() method.
Explanation: Even of multiple runs, it has been observed that static timing is always less than dynamic timing. As static methods are compiled during run time while dynamic one at run time, the latter always encounter overhead which costs the delay at run time and can be seen in our output.
Screenshot and sample run output is also copied at the bottom of this answer.
===============================================================
class Rectangle
{
//Static method for area calculation
static double area_static(double length, double
breadth)
{
return
length*breadth;
}
//Dynamic method for area calculation
double area_dynamic(double length, double
breadth)
{
return
length*breadth;
}
}
class Square extends Rectangle
{
//Overloaded dynamic method for area
calculation
double area_dynamic(double length, double
breadth)
{
return
length*breadth;
}
}
public class Main
{
public static void main(String[] args) {
//Declarations
long startTime, endTime, i;
double duration;
double area;
//Create an object of Square class
with superclass reference
Rectangle square = new
Square();
//Time the for loop for dynamic
calls
startTime =
System.nanoTime();
for(i=0; i<10000000; i++)
{
area =
square.area_dynamic(i,i);
//Dynamically linked
}
endTime = System.nanoTime();
duration = (double)(endTime -
startTime)/1000000; //Convert to milliseconds
//Print execution time for dynamic
execution
System.out.println("Dynamically
bound method execution time: " + duration + " ms");
//Time the for loop for static
calls
startTime =
System.nanoTime();
for(i=0; i<10000000; i++)
{
area =
square.area_static(i,i);
//Statically linked
}
endTime = System.nanoTime();
duration = (double)(endTime -
startTime)/1000000; //Convert to milliseconds
//Print execution time for static
execution
System.out.println("Statically
bound method execution time: " + duration + " ms");
}
}
===============================================================
Code Screenshot:
===============================================================
Output Screenshots:
Run 1:
Run 2:
Run 3:
Get Answers For Free
Most questions answered within 1 hours.