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
Question
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
Solution
The output of the Java program will be 2.
Here's the step by step explanation:
-
Class B extends Class A, which means Class B inherits all the properties and methods of Class A.
-
In the main method, an object 'obj' of Class B is created.
-
The integer 'i' of Class A is set to 1 and the integer 'j' of Class B is set to 2 using the object 'obj'.
-
When the display() method is called using the object 'obj', the display() method of Class B is executed because the object is of Class B.
-
The display() method of Class B prints the value of 'j', which is 2.
So, the output of the program is 2.
Similar Questions
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
Assuming that all the necessary imports are done, what will be the output of the following Java program? class parent { int a; } class child extends parent { int b; void display() { super.a = b + 1; System.out.println(b + " " + i); } } class inheritance { public static void main(String args[]) { child obj = new child(); obj.a=1; obj.b=2; obj.display(); }}Options3 32 22 3Compilation 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
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 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();}}
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.