Examine the following code and select the correct options:
class Main { //from w ww.j av a 2s . co m public Main() { this (7); System.out.println("public"); } private Main(int val) { this ("Sunday"); System.out.println("private"); } protected Main(String val) { System.out.println("protected"); } } public class TestMain { public static void main(String[] args) { Main m = new Main(); } }
a The class Main defines three overloaded constructors. b The class Main defines two overloaded constructors. The private constructor isn't counted as an overloaded constructor. c Constructors with different access modifiers can't call each other. d The code prints the following: // w w w. j ava 2s . c om protected private public e The code prints the following: public private protected
a, d
You can define overloaded constructors with different access modifiers in the same way that you define overloaded methods with different access modifiers.
But a change in only the access modifier can't be used to define overloaded methods or constructors.
private methods and constructors are also counted as overloaded methods.
The following line of code calls Main's constructor, which doesn't accept any method argument:.
Main m = new Main();
The no-argument constructor of this class calls the constructor that accepts an int argument, which in turn calls the constructor with the String argument.
Because the constructor with the String constructor doesn't call any other methods, it prints protected and returns control to the constructor that accepts an int argument.
This constructor prints private and returns control to the constructor that doesn't accept any method argument.
This constructor prints public and returns control to the main method.