What will be the output of the following program code?
class Rectangle{
public int area(int length, int width){
return length*width;
}
}
class Square extends Rectangle{
public int area(long length, long width){
return (int) Math.pow(length, 2);
}
}
public class Test{
public static void main(String args[]){
Square r = new Square();
System.out.println(r.area(5 , 4));
}
}
class Rectangle{
public int area(int length, int width){
return length*width;
}
}
class Square extends Rectangle{
public int area(long length, long width){
return (int) Math.pow(length, 2);
}
}
public class Test{
public static void main(String args[]){
Square r = new Square();
System.out.println(r.area(5 , 4));
}
}
A. Will not compile.
B. Will compile and run printing out 20
C. Runtime error
D. Will compile and run printing out 25
Answer: Option B
Join The Discussion
Comments ( 7 )
What is method overriding in Java?
A. Redefining a superclass method in a subclass
B. Defining a new method with the same name in a subclass
C. Making a method private in a subclass
D. Hiding methods in a superclass
What is the purpose of method overloading in Java?
A. Creating static methods
B. Hiding methods in a superclass
C. Redefining methods in a subclass
D. Defining multiple methods with the same name but different parameters
A. @OverrideMethod
B. @OverrideSuper
C. @Override
D. @OverrideParent
What happens when a subclass tries to override a final method from the superclass in Java?
A. The final method is hidden
B. It results in a compilation error
C. The final method becomes static
D. The final method is hidden
Child class reference can be used to call both parent and child class methods and in this program the arguments passed are int so be default at Run time parent object was been called and output is 20, If the arguments passed were (5l,4l) then the output would be 25. if anyone has a doubt you can compile and check the output.
@Shrey Soni in this program dynamic polymorphism is not taking place and you can check the conditions for overriding a method properly, this is plain inheritance where parent class object is available to child class object and a parent method was called because of Argument type.
The method call is bound to the object at run time, this is due to both present methods and program does not know which method call to bound to.
This leads to the confusion until the objects are created. At creating the method call is bound to parent's method and invokes that method.
This is called Dynamic polymorphism, or late binding, since dynamic polymorphism exploits the overriding.
D is answer ...Because 5^2 =25
The answer is 20 as we are passing int values. The parent class method accepts int values while the child class method accepts long values. Therefore the parent class method is called. If you pass (5L,4L) instead, the child class method is called and the answer comes to 25.
25 IS THE ANS
Math.pow(x,y)= x raise to the power y
(5,2)= 5*5 = 25..
20 seems in correct
Square r equal to new square , so by the reference they call sub class . So answer is 25