Constructor
What is a Constructor in Java?
Constructor is just like a method in Java that is used to initialize the state of an object and will be invoked during the time of object creation.
What are the Rules for defining a constructor?
Constructor name should be the same as the class name
It cannot contain any return type
It can have all Access Modifiers are allowed (private , public, protected, default)
It Cannot have any Non Access Modifiers (final ,static, abstract, synchronized)
No return statement is allowed
It can take any number of parameters
Constructor can throw exception, we can have throws clause
What is the default constructor?
Used to initialize with default values, which will be provided by JVM if programmer is not defining.
What keywords are applicable for constructors?
public , private , protected , default (if we care not mentioning )
Why constructor not return any value?
Constructor will never return any value even void, because the basic aim constructor is to place value in the object. If it will return value then it will become method .
Why constructor definition should not be static?
Constructor definition should not be static because constructor will be called each and every time when object is created. If we made it static then every time object will be created automatically.
Can we inherit constructors? Why is constructor not inherited?
No constructors is not inherited because every class constructor is created for initialize its own data members. The scope of constructor is within the class. So we can’t inherit them.
What is the purpose of the default constructor?
The purpose of default constructor is to create multiple object with respect to same class for placing same value. The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.
Difference between Method and Constructor
Method can be any user defined name
Constructor must be class name.
Method should have return type
It should not have any return type (even void)
Method should be called explicitly either with object reference or class reference
It will be called automatically whenever object is created
Method is not provided by compiler in any case.
The java compiler provides a default constructor if we do not have any constructor.
What is the access modifier of the Default constructor?
It it depends on modifier of class . If the Class declared as public, then the default constructor access modifier will be public.
Can constructor be synchronized in Java?
No. constructor cannot be synchronized.
Can we throw exceptions from a constructor?
Yes, constructor can have throws clause.
What are the access modifiers that cannot be applied to a constructor?
final ,static, abstract and synchronized keywords cannot be applied to a constructor.
Can we declare a constructor as private? Use of private Constructor.
Yes, We can declare constructor as private but if you make any constructor as private then you cannot create object of that class from outside of class. We can also use other access modifiers with constructor like default, public, protected.
What is constructor chaining?
Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor, while super() is used to call super class constructor.
How do I call subclass constructor from superclass constructor?
No. This is not possible.
Do we have destructors in Java?
There is no destructors in java because java is garbage collected language.
Why compiler given constructor is called as default constructor?
Because it obtain all its default properties from its class.
Its accessibility modifier is same as the class .
Its name same as the class .
It does not have parameter and logic .
Can you create an object without using new operator in Java?
Yes, We can create an object without using new operator. Other ways are
Why can constructors not be final in Java?
When you set a method as final, then " The method cannot be overridden by any class" , but Constructor by JLS ( Java Language Specification ) definition can’t be overridden. A constructor is not inherited, so there is no need for declaring it as final.
Why can constructors not be abstract in Java?
When you set a method as abstract, then “The method doesn’t or cannot have a body”. A constructor will be automatically called when an object is created. It cannot lack a body, moreover an abstract constructor could never be implemented.
Why can constructors not be static in Java?
When you set a method as static, it means “The Method belong to class and not to any particular object” but a constructor is always invoked regarding an object, so it makes no sense for a constructor to be static.
Last updated