Knowee
Questions
Features
Study Tools

What will be the output of the following code?class A {    void method() {        System.out.println("Class A");    }}class B extends A {    void method() {        super.method();        System.out.println("Class B");    }}public class Main {    public static void main(String[] args) {        B obj = new B();        obj.method();    }}

Question

What will be the output of the following code?class A {    void method() {        System.out.println("Class A");    }}class B extends A {    void method() {        super.method();        System.out.println("Class B");    }}public class Main {    public static void main(String[] args) {        B obj = new B();        obj.method();    }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be:

Class A Class B

Explanation:

  1. Class B extends Class A, which means it inherits all the methods from Class A.
  2. In the main method, an object of Class B is created.
  3. The method() of Class B is called using the object of Class B.
  4. Inside the method() of Class B, firstly, the method() of Class A is called using the super keyword, which prints "Class A".
  5. After that, "Class B" is printed.
  6. So, the final output is "Class A" followed by "Class B".

This problem has been solved

Similar Questions

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

elect the correct answerPredict the output of the given Java Programs. class A {    public void Print() {        System.out.println("B's Print()");     }  }   class A extends B {    public void Print() {        System.out.println("A's Print()");      }  }   class C extends A {    public void Print() {        super.super.Print();        System.out.println("C's Print()");     }  }   public class Main {    public static void main(String[] args) {         C c = new C();         c.Print();      } }OptionsB's Print()C's Print()A's Print()Compiler Error

What will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options02Compilation Error1

What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }2 23 3Runtime ErrorCompilation Erro

What is the expected output?class Animal { Animal() { System.out.println("Animal"); }}class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); }}public class Test { public static void main(String args[]) { Wild wild = new Wild(); }}

1/3

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.