Knowee
Questions
Features
Study Tools

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

Question

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

...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. The DoPrint method is defined to take a Grandparent object as an argument and call its Print method.

  2. In the main method, three objects are created: a is a Grandparent, b is a Parent but referenced as a Grandparent, and c is a Parent.

  3. When DoPrint(a); is called, it passes the Grandparent object a to the DoPrint method. The Print method of the Grandparent class is called, printing "Grandparent".

  4. When DoPrint(b); is called, it passes the Parent object b (which is referenced as a Grandparent) to the DoPrint method. Since b is actually an instance of Parent, the Print method of the Parent class is called due to Java's runtime polymorphism, printing "Parent".

  5. When DoPrint(c); is called, it passes the Parent object c to the DoPrint method. The Print method of the Parent class is called, printing "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

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

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

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

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

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.