Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - General Questions (Q.No. 5)
5.
public class Test { }
What is the prototype of the default constructor?
Test( )
Test(void)
public Test( )
public Test(void)
Answer: Option
Explanation:

Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).

Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

Discussion:
13 comments Page 1 of 2.

Ashwin said:   1 decade ago
Why the option A is wrong ?
The compiler will implicitly put "public" in front of Test(). Then why this option is wrong.

And wht do you mean by prototype ?

Mahesh said:   1 decade ago
Option A is wrong. The default constructor has the same access modifier as the class. Here the is defined public so default constructor must be public. And Test() is in default access level.

The compiler will not implicitly put "public" in front of Test().

Saritha said:   1 decade ago
If the class having the access modifier as private. Then is it the default constructor of class is private?

Sushil said:   1 decade ago
//I have written simple program
public class Outer
{
Outer(){

System.out.println(" This less access specifier");
}
public static void main(String[] argv)
{
new Outer();
}
}

/*OUTPUT is
This less access specifier
*/
/*This means option A is also correct */
If I am wrong please clarifiy me.

Mohan said:   1 decade ago
What is the prototype of constructor in before creating an object?

Jothi Pandiyan said:   1 decade ago
@Sushil.

In your program you are creating an object and overriding default constructor. Try this, create object without constructor and compile it.

Here compiler creates an constructor with the access modifier same as class access modifier. To check this, use javap command in your command prompt.

//Your program:

public class Outer
{
public static void main(String[] argv)
{ new Outer();
}
}

//compile this source code as
-> javac Outer.java
-> javap Outer

Harshini said:   9 years ago
@Jothi Pandiyan.

What is the meaning of this?

javac Outer.java
java Outer

MSheliga said:   9 years ago
Another way of looking at this is that you can overwrite the default constructor to be default or protected, (and likely even private). I've done this below.

However, if you do *NOT* overwrite the default constructor its public and therefore accessible from anywhere.

public class NewTest
{
public int i = 0;

// protected NewTest() // This compiles even though its protected (not public as class is).
NewTest() // This alsp compiles even though its default (not public as class is).
{
System.out.println("NewTest() new called.");
i = 99;
}

public static void main(String args[])
{
NewTest sub = new NewTest();
System.out.println(sub.i);
}
}
(1)

PRANAY said:   8 years ago
Option A is correct.

And I agree @Jothi Pandiyan.

Shubhamsinh said:   6 years ago
I think Option A is correct.


Post your comments here:

Your comments will be displayed after verification.