Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The output of the Java program will be:

Grandparent Parent Parent

Here's the step by step explanation:

  1. An object 'a' of class Grandparent is created and the DoPrint method is called with 'a' as the argument. The Print method of class Grandparent is called and "Grandparent" is printed.

  2. An object 'b' of class Grandparent is created but it's actually a Parent object because of the "new Parent()" assignment. When the DoPrint method is called with 'b', the Print method of class Parent is called (because 'b' is actually a Parent object) and "Parent" is printed.

  3. An object 'c' of class Parent is created and the DoPrint method is called with 'c' as the argument. The Print method of class Parent is called and "Parent" is printed.

So, the correct option is "Grandparent Parent Parent".

This problem has been solved

Similar Questions

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

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 2Answera.Parentb.Childc.Runtime errord.Compilation error

t the correct answerPredict the output of the given Java Program. class Grandparent {     public void show() {        System.out.println("Grandparent::show() called");     } }   class Parent extends Grandparent {     public void show() {        System.out.println("parent::show() called");    } }   class Main {    public static void main(String[] args) {        Grandparent b = new Parent();;        b.show();    } }OptionsParent::show() calledCompiler Errorgrandparent::show() calledException

elect the correct answerPredict the output of the given Java Programs. class A {    public void Print() {        System.out.println("B's Print()");     }  }   class A extends B {    public void Print() {        System.out.println("A's Print()");      }  }   class C extends A {    public void Print() {        super.super.Print();        System.out.println("C's Print()");     }  }   public class Main {    public static void main(String[] args) {         C c = new C();         c.Print();      } }OptionsB's Print()C's Print()A's Print()Compiler Error

Select the correct answerWhat is the output of the following Java program?class Parent { String name = "parent"; String message() { return "from parent"; }}class Child extends Parent { String name = "child"; String message() { return "from child"; }}public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.name + " " + p.message()); }}Options"parent from parent""parent from child""child from parent""child from child"

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.