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); }}
Solution
The output of the Java program will be:
Grandparent
Parent
Parent
Here's the step-by-step explanation:
-
The
DoPrintmethod is defined to take aGrandparentobject as an argument and call itsPrintmethod. -
In the
mainmethod, three objects are created:ais aGrandparent,bis aParentbut referenced as aGrandparent, andcis aParent. -
When
DoPrint(a);is called, it passes theGrandparentobjectato theDoPrintmethod. ThePrintmethod of theGrandparentclass is called, printing "Grandparent". -
When
DoPrint(b);is called, it passes theParentobjectb(which is referenced as aGrandparent) to theDoPrintmethod. Sincebis actually an instance ofParent, thePrintmethod of theParentclass is called due to Java's runtime polymorphism, printing "Parent". -
When
DoPrint(c);is called, it passes theParentobjectcto theDoPrintmethod. ThePrintmethod of theParentclass is called, printing "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
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
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.