What is the expected output?class Animal { Animal() { System.out.println("Animal"); }}class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); }}public class Test { public static void main(String args[]) { Wild wild = new Wild(); }}
Question
What is the expected output?class Animal { Animal() { System.out.println("Animal"); }}class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); }}public class Test { public static void main(String args[]) { Wild wild = new Wild(); }}
Solution
The code will not compile because you're calling super() after System.out.println("Wild");. In Java, the call to super() must be the first statement in a constructor.
If you move super(); to be the first statement in the Wild constructor, the output will be:
Animal
Wild
This is because when a new instance of Wild is created, the constructor of Animal (the superclass) is called first (due to super();), then the constructor of Wild is executed.
Similar Questions
What will be the output of the following code?class A { void method() { System.out.println("Class A"); }}class B extends A { void method() { super.method(); System.out.println("Class B"); }}public class Main { public static void main(String[] args) { B obj = new B(); obj.method(); }}
What will be the output of the following program ?class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}Select one:a.Class AudiClass CarClass FourWheelerb.Compile Time Errorc.Class FourWheelerClass CarClass Audid.Exception occurs
elect the correct answerPredict the output of the given Java Programs. class A { public void Print() { System.out.println("B's Print()"); } } class A extends B { public void Print() { System.out.println("A's Print()"); } } class C extends A { public void Print() { super.super.Print(); System.out.println("C's Print()"); } } public class Main { public static void main(String[] args) { C c = new C(); c.Print(); } }OptionsB's Print()C's Print()A's Print()Compiler Error
class Animal { public String speak() { return "Some sound"; }}class Dog extends Animal { @Override public String speak() { return "Bark"; }}class Cat extends Animal { @Override public String speak() { return "Meow"; }}public class TestAnimals { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); Animal myCat = new Cat(); System.out.println(myAnimal.speak()); System.out.println(myDog.speak()); System.out.println(myCat.speak()); }}a.Compilation Errorb.Some soundBarkMeowc.Some soundSome soundSome soundd.BarkBarkMeow
What will be the output of the program?class Super{ public int i = 0; public Super(String text) /* Line 4 */ { i = 1; } } class Sub extends Super{ public Sub(String text) { i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } }012Compilation fails.
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.