Java Programming - Declarations and Access Control - Discussion
Discussion Forum : Declarations and Access Control - Pointing out the correct statements (Q.No. 4)
4.
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.
Answer: Option
Explanation:
(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.
Discussion:
6 comments Page 1 of 1.
Prajwal said:
7 years ago
5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.
5) The compiler creates a default constructor only when there are no other constructors for the class.
Only is the difference. The default constructor is created when there is no param constructor defined.
5) The compiler creates a default constructor only when there are no other constructors for the class.
Only is the difference. The default constructor is created when there is no param constructor defined.
Pavani said:
1 decade ago
Because 0 is default value for integer. Whenever object is created then memory allocated to instance variables (if there is no initialization for those variables then it takes default values) at the same time constructor also called.
so B and C gets directly 10 and 20 and there is no value for A, so it gets default value.
so B and C gets directly 10 and 20 and there is no value for A, so it gets default value.
Ankit said:
1 decade ago
Please explain me this :
5. The compiler creates a default constructor only when there are no other constructors for the class.
class A{
int a;
int b;
int c;
A(int b,int c){
this.b=b;
this.c=c; }}
class B{
public static void main(String args [ ]){
A obj = new A(10,20);
System.out.println("a="+obj.a+" b="+obj.b+" c="+obj.c);
}
}
Output:a=0 b=10 c=20
How does variable a is assigned a value 0 if there is no default constructor made by the compiler ?
5. The compiler creates a default constructor only when there are no other constructors for the class.
class A{
int a;
int b;
int c;
A(int b,int c){
this.b=b;
this.c=c; }}
class B{
public static void main(String args [ ]){
A obj = new A(10,20);
System.out.println("a="+obj.a+" b="+obj.b+" c="+obj.c);
}
}
Output:a=0 b=10 c=20
How does variable a is assigned a value 0 if there is no default constructor made by the compiler ?
Iroshan said:
1 decade ago
Default constructor can be private as well.
Tiago said:
1 decade ago
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.
This is wrong. CoffeeCup on the example is default access.
This is wrong. CoffeeCup on the example is default access.
Stinger said:
1 decade ago
In the above explanation, the class has default access but the constructor is public?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers