What happens when the following program is compiled and run?public abstract class MyAbstract { abstract int getSum();abstract int getSum(int x);abstract int getSum(int x, int y);} public abstract class Sub extends MyAbstract { int x = 4; int getSum(){return x + x;}int getSum(int x){return x + x;}int getSum(int x, int y){int z = x + y;System.out.print(z);return z;}public static void main(String[] args){Sub sub = new Sub();sub.getSum(4,2);}}
Question
What happens when the following program is compiled and run?public abstract class MyAbstract { abstract int getSum();abstract int getSum(int x);abstract int getSum(int x, int y);} public abstract class Sub extends MyAbstract { int x = 4; int getSum(){return x + x;}int getSum(int x){return x + x;}int getSum(int x, int y){int z = x + y;System.out.print(z);return z;}public static void main(String[] args){Sub sub = new Sub();sub.getSum(4,2);}}
Solution
The code you provided will not compile. The reason is that you have declared the class Sub as abstract but you are trying to create an instance of it in the main method. Abstract classes in Java cannot be instantiated. They are meant to be extended by other classes.
If you want to run this code, you need to remove the abstract keyword from the Sub class declaration. However, even after doing that, you will still encounter a problem because the Sub class is extending an abstract class MyAbstract but it's not providing the implementation for all the abstract methods declared in MyAbstract.
In your Sub class, you need to provide the implementation for all the abstract methods declared in MyAbstract class.
Here is the corrected code:
public abstract class MyAbstract {
abstract int getSum();
abstract int getSum(int x);
abstract int getSum(int x, int y);
}
public class Sub extends MyAbstract {
int x = 4;
int getSum() {
return x + x;
}
int getSum(int x) {
return x + x;
}
int getSum(int x, int y) {
int z = x + y;
System.out.print(z);
return z;
}
public static void main(String[] args) {
Sub sub = new Sub();
sub.getSum(4,2);
}
}
When you run this program, it will print 6 to the console because it's the sum of 4 and 2 which are the arguments passed to the getSum(int x, int y) method.
Similar Questions
What is the output of the following code? 1: public abstract class Whale { 2: public abstract void dive() {}; 3: public static void main(String[] args) { 4: Whale whale = new Orca(); 5: whale.dive(); 6: } 7: } 8: class Orca extends Whale { 9: public void dive(int depth) { System.out.println("Orca diving"); } 10: }*1 pointA. Orca divingB. The code will not compile because of line 2.C. The code will not compile because of line 8.D. The code will not compile because of line 9.E. The output cannot be determined from the code provided.
What is the Output of following Java Program?abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); }class Test extends Demo{ final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception
What will be printed after executing following program code?class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; }}class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; }}public class Test{ public static void main(String[] args){ Base b = new Derived(); System.out.pr 10203040
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3
What all gets printed when the following program is compiled and run.public class Test{ public static void main(String args[]){ int i, j=1; i = (j>1)?2:1; switch(i){ case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } }}01212
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.