public class ArithmeticOperation{ private void add(int operand1, int operand2) { System.out.println(operand1 + operand2); }}public class Addition extends ArithmeticOperation{ public void show() { add(10, 12); } public static void main(String args[]) { Addition ob = new Addition(); ob.show(); }}What will be the output of above code when compiled and executed?Select one:a.Runtime error as add method is not defined in MethodOverriding classb.Will print falsec.Will compile and display 1012d.Compile time errore.Will compile and display 32
Question
public class ArithmeticOperation{ private void add(int operand1, int operand2) { System.out.println(operand1 + operand2); }}public class Addition extends ArithmeticOperation{ public void show() { add(10, 12); } public static void main(String args[]) { Addition ob = new Addition(); ob.show(); }}What will be the output of above code when compiled and executed?Select one:a.Runtime error as add method is not defined in MethodOverriding classb.Will print falsec.Will compile and display 1012d.Compile time errore.Will compile and display 32
Solution
The output of the above code when compiled and executed will be a compile-time error.
Here's why:
The add method in the ArithmeticOperation class is declared as private. This means it can only be accessed within the ArithmeticOperation class itself. It cannot be accessed from any other class, including subclasses.
In the Addition class, which extends ArithmeticOperation, the show method is trying to call the add method. However, because add is private, this is not allowed, and the code will not compile.
So, the correct answer is d. Compile time error.
Similar Questions
Select the correct answerWhat will be the output of the following Java code? class overload { int Code; int Tantra; void add(int x) { Code = x + 1; } void add(int x, int y) { Code = x + 2; } } class Overload_methods { public static void main(String args[]) { overload obj = new overload(); int Code = 0; obj.add(6); System.out.println(obj.Code); } }Options7658
What is the output of the following code?1. public class A {2. int add(int i, int j){3. return i+j;4. }5. }6. public class B extends A{7. public static void main(String argv[]){ 8. short s = 9;9. System.out.println(add(s,6));10. }11.}
class Calculator { public static int add(int a, int b) { return a + b; } public static int add(int a, int b, int c) { return a + b + c; }}public class Main { public static void main(String[] args) { int result1 = Calculator.add(10, 20); int result2 = Calculator.add(10, 20, 30); System.out.println("Result1: " + result1 + ", Result2: " + result2); }}a.Result1: 30, Result2: 60b.Result1: 20, Result2: 30c.None of the aboved.Compilation Error
What will be the output?interface A{ public void method1();}class One implements A{ public void method1(){ System.out.println("Class One method1"); }}class Two extends One{ public void method1(){ System.out.println("Class Two method1"); }}public class Test extends Two{ public static void main(String[] args){ A a = new Two(); a.method1(); }} Class One method1Class Two method1Nothing will be printed Compilation Error
Select the correct answerWhat will be the output of the following Java program? final class base { int a; } class derived extends base { int b; System.out.println(b + " " + a); } class inheritance { public static void main(String args[]) { derived obj = new derived(); obj.display(); } }OptionsRuntime Error3 32 2Compilation Error
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.