What is the output of the following code snippet?class Parent { static void print() { System.out.println("Parent"); }}class Child extends Parent { static void print() { System.out.println("Child"); }}public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.print(); }}Question 3Answera.Parentb.Runtime errorc.Childd.Compilation error
Question
What is the output of the following code snippet?class Parent { static void print() { System.out.println("Parent"); }}class Child extends Parent { static void print() { System.out.println("Child"); }}public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.print(); }}Question 3Answera.Parentb.Runtime errorc.Childd.Compilation error
Solution
The output of the code snippet will be "Parent".
Here's the step by step explanation:
-
The
Parentclass has a static methodprint()which prints "Parent". -
The
Childclass extendsParentand it also has a static methodprint()which prints "Child". -
In the
mainmethod, aParentreference variableobjis created and it is assigned a newChildobject. -
When
obj.print()is called, it calls theprint()method of theParentclass, not theChildclass. This is because static methods are not part of an object's state. They belong to the class itself and are bound at compile time, not at runtime. This is known as static binding or early binding.
So, the correct answer is a. "Parent".
Similar Questions
Output of following Java program?class Grandparent { public void Print() { System.out.println("Grandparent"); } } class Parent extends Grandparent { public void Print() { System.out.println("Parent"); }} class Main{ public static void DoPrint( Grandparent o ) { o.Print(); } public static void main(String[] args) { Grandparent a = new Grandparent(); Grandparent b = new Parent(); Parent c = new Parent(); DoPrint(a); DoPrint(b); DoPrint(c); }}OptionsGrandparentGrandparentParentGrandparentParentParentGrandparentParentGrandparentCompiler Error
Observe the below code :class Parent { public void display() { System.out.println("Parent"); }}class Child extends Parent { public void display() { System.out.println("Child"); }}public class Main { public static void main(String args[ ]) { Parent p = ; Child c = ; p.display(); c.display(); }}Fill the code accordingly, so that you get the output asChildChild.
What will be the output of the following code snippet?public class MyClass { static int value = 10; public void printValue() { System.out.println(value); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.printValue(); }}Question 4Answera.0b.10c.Compilation errord.Runtime exception
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
Select the correct answerWhat is the output of the following java program?class Parent { public final void show() { System.out.println("Parent::show() called"); }}class Child extends Parent { public void show() { System.out.println("Child::show() called"); }}public class Main { public static void main(String[] args) { Parent p = new Child();; p.show(); }}OptionsCompiler ErrorBase::show() calledDerived::show() calledException
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.