Knowee
Questions
Features
Study Tools

What is the output of the following Java code?Code:class HackerEarth{ public void print() { System.out.println("Inside HackerEarth"); }}class Hacker extends HackerEarth{ public void print() { System.out.println("Inside Hacker"); }}public class HackerEarthMain{ public static void main(String[] args) { HackerEarth obj1 = new HackerEarth(); Hacker obj2 = new Hacker(); ((HackerEarth) obj2).print(); obj1 = obj2; obj1.print(); }}

Question

What is the output of the following Java code?Code:class HackerEarth{ public void print() { System.out.println("Inside HackerEarth"); }}class Hacker extends HackerEarth{ public void print() { System.out.println("Inside Hacker"); }}public class HackerEarthMain{ public static void main(String[] args) { HackerEarth obj1 = new HackerEarth(); Hacker obj2 = new Hacker(); ((HackerEarth) obj2).print(); obj1 = obj2; obj1.print(); }}

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

Solution

The output of the Java code will be:

Inside Hacker Inside Hacker

Explanation:

In the first call, ((HackerEarth) obj2).print();, even though obj2 is cast to HackerEarth, the actual object is of type Hacker. So, it will call the print method in the Hacker class.

In the second call, obj1 = obj2; obj1.print();, obj1 is now referring to obj2 which is of type Hacker. So, it will again call the print method in the Hacker class.

This problem has been solved

Similar Questions

What is the output of the following Java code?Code:class HackerEarth{ public static void main(String[] args) { int val = 1; while((val<=5 ? val++: --val)<5) { System.out.print("MAC"); } if(val>5); System.out.print("PC"); }}

What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world";   String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}

What is the output of the following Java code:Code:class hacker{ public static void main(String[] arg) { int[] a = { 128, 132, 98, 116, 100 }; int x = func(a); System.out.println(x); } public static int func(int[] no) { int b = no[0]; for (int i: no) { if (i > b) b = i; } return b; }}

e correct answerWhat will be the output of the following Java code?  class Codetantra  {    final public int output(int x, int y) { return 1; }   }   class Program extends Codetantra   {     public int output(int x, int y) { return 2; }   }    public class result   {    public static void main(String args[])     {       Program object = new Program();       System.out.print("y is " + y.output(0, 1));      }   }

What is the output of the following code?public class Main {  public static void main(String[] args) {    String str1 = "NPTEL";    String str2 = "java";    int a = 20;    int b = 24;    System.out.println(str1 + a + b); // Statement 1     System.out.println(a + b + str2); // Statement 2  }} a. NPTEL2024         44java b. NPTEL44         44java c. NPTEL2024         2024java d. NPTEL44         2024java

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.