A

Admin • 833.24K Points
Coach

Q. What is the result of the following code snippet?

class Parent {
private int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
System.out.println(super.x + " " + x);
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}

  • (A) 20 10
  • (B) 10 20
  • (C) Runtime exception
  • (D) None of These
  • Correct Answer - Option(B)
  • Views: 36
  • Filed under category Java
  • Hashtags:

Explanation by: Admin

The given code snippet involves inheritance in Java. It defines two classes: Parent and Child. The Child class extends the Parent class, which means it inherits its fields and methods.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.