Consider the following code appearing in Bird.java
class Animal { /*from w w w .j a v a 2s .c om*/ private Animal (){ } } class Bird extends Animal { public String name; public Bird (String name){ this.name = name; } public static void main (String [] args) { System.out.println (new Bird ("Bald Bird").name); } }
What needs to be done to make this code compile?
Select 1 option
A. Nothing, it will compile as it is. /*from www.j a v a 2 s . c o m*/ B. Make Bird class declaration public: public class Bird { ... } C. Make the Bird constructor private: private Bird (String name){ ... } D. Make Animal constructor public: public Animal () { ... } E. Insert super(); as the first line in Bird constructor : public Bird (String name){ super (); this.name = name; }
Correct Option is : D
If a subclass class constructor doesn't explicitly call the super class constructor, the compiler automatically inserts super()
; as the first statement of the base class constructor.
So option 5 is not needed.
Since the constructor of Animal is private, the subclass cannot access it and therefore, it needs to be made public.