1. Which three statements are true? The default constructor initialises method variables. The default constructor has the same access as its class. The default constructor invokes the no-arg constructor of the superclass. If a class lacks a no-arg constructor, the compiler always creates a default constructor. The compiler creates a default constructor only when there are no other constructors for the class.
Ask Your Doubts Here
Comments
By: guest on 02 Jun 2017 01.25 am
(2) sounds correct as in the example below class CoffeeCup { private int innerCoffee; public CoffeeCup() { } public void add(int amount) { innerCoffee += amount; } //... } The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well. (3) is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "<init>." For each constructor in the source code of a class, the Java compiler generates one <init>() method. If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclass's no-arg constructor. As with any other constructor, the compiler creates an <init>() method in the class file that corresponds to this default constructor. (5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.