What is the Output of the following Java program?
RUN CODE ONLINEclass First { int i = 10; public First(int j) { System.out.println(i); this.i = j * 10; } } class Second extends First { public Second(int j) { super(j); System.out.println(i); this.i = j * 20; } } public class MainClass { public static void main(String[] args) { Second n = new Second(20); System.out.println(n.i); } }
Output
10 200 400 Since Second class doesn't have its own "i" variable, it inherits it from the super class. Also, the constructor of Second is called when we create an object of Parent.
Leave a Reply