Knowee
Questions
Features
Study Tools

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

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

Solution

The output of the code snippet will be "Parent".

Here's the step by step explanation:

  1. The Parent class has a static method print() which prints "Parent".

  2. The Child class extends Parent and it also has a static method print() which prints "Child".

  3. In the main method, a Parent reference variable obj is created and it is assigned a new Child object.

  4. When obj.print() is called, it calls the print() method of the Parent class, not the Child class. 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".

This problem has been solved

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

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.