Observe the below code :class Parent { public void display() { System.out.println("Parent"); }}class Child extends Parent { public void display() { System.out.println("Child"); }}public class Main { public static void main(String args[ ]) { Parent p = ; Child c = ; p.display(); c.display(); }}Fill the code accordingly, so that you get the output asChildChild.
Question
Observe the below code :class Parent { public void display() { System.out.println("Parent"); }}class Child extends Parent { public void display() { System.out.println("Child"); }}public class Main { public static void main(String args[ ]) { Parent p = ; Child c = ; p.display(); c.display(); }}Fill the code accordingly, so that you get the output asChildChild.
Solution
The code should be filled as follows:
class Parent {
public void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
public void display() {
System.out.println("Child");
}
}
public class Main {
public static void main(String args[ ]) {
Parent p = new Child();
Child c = new Child();
p.display();
c.display();
}
}
In this code, we are creating objects of Child class and assigning them to Parent and Child class references. When we call the display() method, it calls the Child class's display() method because of the concept of method overriding in Java. This is why the output will be Child Child.
Similar Questions
elect the correct answerWhat will be the output of the following Java program? class parent { int a; public void display() { System.out.println(a); } } class child extends parent { int b; public void display() { System.out.println(b); } } class Dynamic_dispatch { public static void main(String args[]) { child obj2 = new child(); obj2.a = 2; obj2.b = 4; parent r; r = obj2; r.display(); } }Options6428
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
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
the correct answerAssuming 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(); }}
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"
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.