Create a class called ZeroSum.java. Take all of the methods from OverloadingSum.
ID: 3763820 • Letter: C
Question
Create a class called ZeroSum.java. Take all of the methods from OverloadingSum.java and change them to return all zeros in ZeroSum.java. Next, create a class called RealSum.java. This class, RealSum, will extend the ZeroSum class. Having all zeros for our sums isn't very useful. We will need to override the parent, or super class methods to produce real results. Create the necessary methods to override all of those in the ZeroSum.java. When you are done run your classes against DrivingSum.java. How to do the bold part, just to mention I did the following : public double sum(short x, double y, double z)
{
return 0; }
OverloadingSum. >> public double sumy(short x, double y, double z){
return x+ y + z;
} < This is done for 68 different versions of the sum method .
Explanation / Answer
Program code:
//OverloadingSum class
public class OverloadingSum //Base class
{
public double sumy(short x, double y, double z){
return x+ y + z;
}
}
//zeroSum class
public class ZeroSum extends OverloadingSum// inherit OverloadingSum class
{
public double sumy(short x, double y, double z)//over riding the method in base class OverloadinSum
{
return 0;
}
}
// RealSum class
public class RealSum extends ZeroSum//inherit ZeroSum class
{
public double sumy(short x, double y, double z)//over ridden method
{
return x+ y + z;
}
}
DrivingSum.java
public class DrivingSum
{
public static void main(String args[])
{
short a=10;
double b=20;
double c=30;
ZeroSum z1=new ZeroSum();//ZeroSum reference and the same class object
ZeroSum r1=new RealSum();//ZeroSum reference but RealSum Object
System.out.println("Value from over-ridden method in ZeroSum class:"+z1.sumy(a, b, c));
System.out.println("Value from over-ridden method in RealSum class:"+r1.sumy(a, b, c));
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.