Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The output of the given Java code will be "2".

Here's the step by step explanation:

  1. An abstract class 'One' is defined with an integer 'i' and an abstract method 'display()'.

  2. A class 'Two' is defined which extends the abstract class 'One'. It has an integer 'j' and it provides the implementation of the abstract method 'display()' from class 'One'. In this method, it prints the value of 'j'.

  3. In the main method of the 'AbstractDriver' class, an object 'obj' of class 'Two' is created.

  4. The value of 'j' for the object 'obj' is set to 2.

  5. The 'display()' method of the object 'obj' is called, which prints the value of 'j', which is 2.

So, the output will be "2".

This problem has been solved

Similar Questions

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

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 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

What will be the output of the following code?This question is required.*class Base {public void display() {System.out.println("Base");}}class Derived extends Base {public void display() {System.out.println("Derived");}}public class Main {public static void main(String[] args) {Base b = new Derived();b.display();}}

What is the output of the below Java program with an abstract class?abstract class MathLibrary{ static final float PI = 3.1415f;}public class AbstractClassTesting3{ public static void main(String[] args) { System.out.println(MathLibrary.PI); }}A) 3.1415B) No OutputC) ErrorD) None of the above

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.