info@meritcampus.com    +91-85006-22255
...
`

Define the classes `Cube` and `PowerOf4` such that it overrides the method `getValue()` and prints the result.

Class used, Input Output
Cube, 2 8
Cube, -17 -4913
PowerOf4, 5 625
PowerOf4, 25 390625

``` class OverridePowersOperation {     public static void main(String s[])    {        Square cube = new Cube();        System.out.println("The cube of 2 is " + cube.getValue(2));        Square power4 = new PowerOf4();        System.out.println("The fourth power of 5 is " + power4.getValue(5));    } } class Square {     int getValue(int a)     {         return a * a ;     } } class Cube extends Square { ```
``` } class PowerOf4 extends Square { ```
``` } ```
Topic:

User comments below.All of them might not be correct.

according to the requirement in the question .. we need to override getValue() method in Cube class and Square Class.. in cube class.. we need to return " cube of a ".. So return a*a*a in cube class getValue() method...
similarly in PowerOf4 class.. we need to return " 4th power of a ".. So return a*a*a*a in PowerOf4 class getValue() method ...

Posted by ?????????? ?????    2015-01-20 05:44:12

Here In main() method we are creating Object for Class Cube and which is stored in the reference variable of Class Square with name as "cube" ... here Class Cube is sub class of Class Square if you create Object of sub class then super class object will create automatically bcz in sub class constructor we have one default statement is super() due to this statement it will cal the super class constructor .....                              if you want to get CUBE of 2                              1. we have to override the method getValue(int a) inside the Class Cube with one return stmt with a*a*a...                              if you cal getValue(2) method with return type as int then it will cal the sub class method if its not there only it will go to super class method bcz sub class methods will cal first  so it will return CUBE of 2 and in simillar way                      after that we are creating Object of Class PowerOf4 and it we are storing inside the reference variable of Class Square so                                     2.we have to write override method with same method name getValue(5)  with return type as int with one return statement a*a*a*a....    so simillaly it will return the 4th power of a value

Posted by Bhagi Bhagyasri    2015-01-20 06:06:32

Here The concept of "METHOD OVERRIDING" should be used to achieve the output desired in program.

--Method Overriding:--> It means defining te diffrent version of method in different classes with same name.Here "Inheritance" must be used to achieve method overriding.so,whenever object of that class passed in baseclass instance then it's method will be called.

PROGRAM HINT:
_____________

To perform the cube of a number we use the "Math.pow()" function which is static method that returns double value.

Declaration:
Following is the declaration for java.lang.Math.pow() method

public static double pow(double a, double b)

Parameters:
a -- the base.

b -- the exponent.

Return Value:
This method returns the value a^b.

>>>>>>For Cube

int getvalue(int a):--> method signature should be same as that of square class to acheive the "METHOD OVERRIDING".

return (int)Math.pow(a);//Here pow() returns double value,it should be converted into integer by "EXPLICIT TYPECASTING."

>>>>>>For Power4

int getvalue(int a):--> method signature should be same as that of square class to acheive the concept of "METHOD OVERRIDING".

return (int)Math.pow(a,4);//Here pow() returns double value,it should be converted into integer by "EXPLICIT TYPECASTING."

Posted by Mânïshå Mùlchåndânï    2015-01-20 16:57:02

This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Second Correct Comment'. The 'lucky liker' is Bhagi Bhagyasri. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-21 07:10:14