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"child from child""parent from parent""parent from child""child from parent"

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"child from child""parent from parent""parent from child""child from parent"

...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, fields are not overridden like methods. So when you access a field, it will access the field of the type of the reference. In this case, the reference type is Parent, so it will access the 'name' field in Parent class, which is "parent".

However, methods are overridden. So when you call a method, it will call the method of the actual object type. In this case, the actual object is of type Child, so it will call the 'message' method in Child class, which returns "from child".

Therefore, the output will be "parent from child".

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

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

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

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.