Knowee
Questions
Features
Study Tools

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"

Question

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"

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

Solution

The correct answer is "parent from child".

Here's why:

In Java, instance variables are not overridden like methods. So, when you create an object of type Parent but instantiate it as a Child (Parent p = new Child()), the instance variable 'name' from the Parent class is used, not the one from the Child class. This is why 'parent' is printed instead of 'child'.

However, methods are overridden. So, when you call p.message(), the method from the Child class is used, not the one from the Parent class. This is why 'from child' is printed instead of 'from parent'.

This problem has been solved

Similar Questions

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

Select the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java code? class parent {  int x;     void display() {      System.out.println(x);     } }     class child extends parent {     int y;     void display() {         System.out.println(y);     } }     class method_overriding {     public static void main(String args[]) {          child obj = new child();         obj.x=4;         obj.y=5;            obj.display();          } }Options435Compilation Error

Select the correct answerWhat is the output of the following Java program?public class Vehicle { public void move() { System.out.println("The vehicle moves"); }}public class Car extends Vehicle { public void move() { System.out.println("The car moves"); }}public class Main { public static void main(String[] args) { Vehicle vehicle = new Car(); vehicle.move(); }}Options"The car moves""The vehicle moves"The code does not compileNone of these

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

Select the correct answerWhat will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options102Compilation 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.