Aryan PrajapatKnowledge Contributor
Identify the output of the below Java program and Justify your answer. class Main { public static void main(String args[]) { Simplilearn s = new Simplilearn(5); } } class InterviewBit{ InterviewBit(){ System.out.println(" Welcome to InterviewBit "); } } class Simplilearn extends InterviewBit{ Simplilearn(){ System.out.println(" Welcome to Simplilearn "); } Simplilearn(int x){ this(); super(); System.out.println(" Welcome to Simplilearn 2"); } }
Identify the output of the below Java program and Justify your answer. class Main { public static void main(String args[]) { Simplilearn s = new Simplilearn(5); } } class InterviewBit{ InterviewBit(){ System.out.println(" Welcome to InterviewBit "); } } class Simplilearn extends InterviewBit{ Simplilearn(){ System.out.println(" Welcome to Simplilearn "); } Simplilearn(int x){ this(); super(); System.out.println(" Welcome to Simplilearn 2"); } }
The above code will throw the compilation error. It is because the super() is used to call the parent class constructor. But there is the condition that super() must be the first statement in the block. Now in this case, if we replace this() with super() then also it will throw the compilation error. Because this() also has to be the first statement in the block. So in conclusion, we can say that we cannot use this() and super() keywords in the same block.