In: Computer Science
Override the methods below (in SBox). The overridden methods should perform the actions mentioned below and it and must display the following message “Overridden method X”, where X is the method name.
a. setlength(int x ), to set the length
b. setWidth (int y ) to set the width
c. setHeight (int z) to set the height & d. displayCalculatedVolume( ) that calculates the volume and displays it.
/*Box is the base class and Sbox is the derived class*/
//importing packages
import java.util.*;
import java.lang.*;
import java.io.*;
class Box{
public void setLength(int x){
System.out.println("Length is "+x);
}
public void setWidth(int y){
System.out.println("Width is "+y);
}
public void setHeight(int z){
System.out.println("Height is "+z);
}
}
class Sbox extends Box{
int l,h,w;
public void setLength(int x){
System.out.println("Overidding method setLength..");
l = x;
}
public void setWidth(int y){
System.out.println("Overidding method setWidth..");
h = y;
}
public void setHeight(int z){
System.out.println("Overidding method setHeight..");
w = z;
}
public void displayCalculatedVolume(int x, int y, int z){
int vol;
setLength(x);
setWidth(y);
setHeight(z);
vol = l*h*w;
System.out.println("Volume is " + vol);
}
public static void main(String args[]){
Sbox obj = new Sbox();
obj.displayCalculatedVolume(2,3,6);
}
}
Output screenshot:
Note