Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The output will be "Class Two method1".

Here's the step by step explanation:

  1. An interface A is declared with a method named method1().

  2. Class One implements interface A and provides an implementation for method1(). When this method is called, it will print "Class One method1".

  3. Class Two extends Class One and overrides the method1(). When this method is called, it will print "Class Two method1".

  4. In the main method of the Test class, an instance of Class Two is created but the reference is of type interface A.

  5. When method1() is called on this reference, the overridden version in Class Two is executed because the actual object is of Class Two. Hence, "Class Two method1" is printed.

This problem has been solved

Similar Questions

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();    }}

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

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

the correct answerWhat will be the output of the given Java program? abstract class java {    int x;    abstract void display(); }     class method extends java {     int y;     void display() {        System.out.println(y);     } }     class Abstract_demo {     public static void main(String args[]) {         method obj = new method();         obj.y = 1;         obj.display();         } }Options0Runtime Error1Compilation Error

Find the output: abstract class One { int i; abstract void display(); } class Two extends One { int j; void display() { System.out.println(j); } } class AbstractDriver { public static void main(String args[]) { Two obj = new Two(); obj.j=2; obj.display(); } }

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.