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?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.
Soumyaranjan Rout said:
3 years ago
As you said, the default constructor has the same access modifier as the class.
If a class is declared as final, what will be the access modifier of the default constructor.
If a class is declared as final, what will be the access modifier of the default constructor.
Soumyaranjan Rout said:
3 years ago
As you said, the default constructor has the same access modifier as the class.
If a class is declared as final then what will be the access modifier of the default constructor.
If a class is declared as final then what will be the access modifier of the default constructor.
Soumik Sarkar said:
4 years ago
In the above example, Outer () is a non-parameterised constructor. Not a default constructor. We can not initialize the default constructor. It will automatically initialize by the compiler itself when we can not define a constructor in a class.
Shubhamsinh said:
6 years ago
I think Option A is correct.
PRANAY said:
8 years ago
Option A is correct.
And I agree @Jothi Pandiyan.
And I agree @Jothi Pandiyan.
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);
}
}
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)
Harshini said:
9 years ago
@Jothi Pandiyan.
What is the meaning of this?
javac Outer.java
java Outer
What is the meaning of this?
javac Outer.java
java Outer
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
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
Mohan said:
1 decade ago
What is the prototype of constructor in before creating an object?
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers