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
πŸ’¬ Discuss
βœ… Correct Answer: (B) 10 20
Explanation: 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.
Explanation by: Sonali Mishra
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
142
Total Visits
πŸ“½οΈ
1 y ago
Published
πŸŽ–οΈ
Sonali Mishra
Publisher
πŸ“ˆ
98%
Success Rate