Assuming that all the necessary imports are done, what will be the output of the following Java code? class codetantra { public int a; public int b; codetantra() { a = 5; b = 6; } } class coding extends codetantra { int i; coding() { super(); } } class inheritance { public static void main(String args[]) { coding obj = new coding(); System.out.println(obj.a + " " + obj.b); } }Options6 55 6Runtime Error
Question
Assuming that all the necessary imports are done, what will be the output of the following Java code? class codetantra { public int a; public int b; codetantra() { a = 5; b = 6; } } class coding extends codetantra { int i; coding() { super(); } } class inheritance { public static void main(String args[]) { coding obj = new coding(); System.out.println(obj.a + " " + obj.b); } }Options6 55 6Runtime Error
Solution
The output of the given Java code will be "5 6".
Here's the step by step explanation:
-
The class
codetantrahas two public integer variablesaandb. In the constructor ofcodetantra,ais assigned the value 5 andbis assigned the value 6. -
The class
codingextendscodetantra, which means it inherits the properties ofcodetantra. Therefore, an object ofcodingwill have access to variablesaandb. -
In the
mainmethod of theinheritanceclass, an objectobjof classcodingis created. -
The
printlnstatement prints the values ofaandbof theobjobject, which are 5 and 6 respectively.
So, the output will be "5 6".
Similar Questions
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
Assuming that all the necessary imports are done, what will be the output of the following Java code? class parent { int x; void display() { System.out.println(x); } } class child extends parent { int y; void display() { System.out.println(y); } } class method_overriding { public static void main(String args[]) { child obj = new child(); obj.x=4; obj.y=5; obj.display(); } }Options435Compilation 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
the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java code? class ct { public int x; private int y; } class uv extends ct { void display() { super.y = super.x + 1; System.out.println(super.x + " " + super.y); } } class inheritance { public static void main(String args[]) { uv obj = new uv(); obj.x = 1; obj.y = 2; obj.display(); } }Options2 2Runtime Error3 3Compilation Error
e correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }
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.