Constructor in Java
Constructor in java is like a method which is used to create instance of a class. In simple words Constructor constructs object.
Constructor in java is like a method which is used to create instance of a class. In simple words Constructor constructs object.
It provides the simplest way to create object with new keyword.
Constructor is declared with the same name as its class name.
Constructor is declared with the same name as its class name.
Types:-
1)parameterized Constructor :- Constructor with arguments2)Default Constructor :- Constructor without arguments
Ex:-
public class Test { public int count; private Test(int count) //parameterized Constructor { this.count = count; } public Test() //Default Constructor { } } |
->constructor can not be inherited.
->Interface cannot have constructor
Why Interface cannot have constructor ?
Ans:- Interface cannot be instantiated.i.e. we cannot create an object of Interface.
Note:- But a Abstract class can have constructor.
Reason:- constructor is used to initialized member variables. We can have member variables in Abstract class which can be initialized in constructor.
Though we can have member variables in Interface those are static and final by default. Which can not be initialized in constructor and has to be initialized during the time of declaration.
Member variables in Interface could have been initialized in static initilizer block, but we can NOT have static initilizer block in Interface. So we have to initialize it during the time of declaration.
Hence we don't need a constructor in Interface.
Q)Here the question arises is we cannot create an Instance of a Abstract class. Then how to call the constructor of it.
Ans:- The constructor of abstract class can be called from the constructor of implementing class using super keyword. Example of super has been given below.
->A constructor cannot be abstract, static, final, native, strictfp, or synchronized.
Note:-Only access specifiers are allowed for constructor are public, protected & private
Why constructor cannot be abstract?
Ans:- When we declare a method abstract i.e. the method cannot have body. But we use constructor to create objects and initialize instance variables which contradicts with the property of abstract keyword.
Why constructor cannot be static?
Ans:- We use static keyword when we want to declare a class variable or method at class level which will be invoked by using Class name not by using objects.
Though syntax of a constructor is like a method it is not a member of a class (unlike instance variable, class variables or methods ). constructor can not be accessed using Object or Class itself. So it makes no sense to declare a constructor static.
Why constructor cannot be inherited?
Ans:- Only methods and instance variables can be inherited in Java. Since constructor is neither of them we can not inherit it. In order to use super class constructor we use "super" keyword.
Ex:-
public class Test extends TestParent
{
Test()
{
super();
}
}
|
Why constructor cannot be final?
Ans:- When we declare a method as final it can not be inherited Anyways we cannot inherit constructor. So there is no point making a constructor final
Why constructor cannot be synchronized?
Ans:- synchronized keyword is used in multithreaded application for shared resources. When we make any method or block synchronized only one thread can access it at a time. If we would make a constructor synchronized then the object will be created one after another in queue which will not help us anyway rather it hinders the performance.
-> Constructor cannot have return type. If we will try to return something from a constructor it will consider it as a method and will through a warning.
Though it will not throw a compile time error, it will throw a warning to change it to constructor.
Ex-
public class Test
{
public int count;
private Test() {
}
public void Test(int count) {
this.count = count;
System.out.println("count is " + this.count);
}
public static void main(String[] args) {
Test test1 = new Test();
test1.Test(5);
}
}
|
The above program will compile successfully with a warning.
Output:- count is 5
->Java provides us default constructor but if we will declare a parameterized constructor then we have to explicitly write the default constructor to access it otherwise it will throw a compilation error.
Ex:-
public class Test
{
public static void main(String[] args) {
Test test1 = new Test();
}
}
|
The above code will compile successfully. But if we will add a parameterized constructor it will throw a compile time error.
Ex:-
public class Test
{
Test(int i) {}
public static void main(String[] args) {
Test test1 = new Test(); // Compilation error will be thrown at this
// line
}
}
|
Ex:-
How to call a constructor from another?
Ans:- A constructor can be called from another constructor by using "this" or "super" keyword. But this will be the first line inside the constructor.
public class Test
{
Test()
{
this(3);
}
Test(int i) {
}
}
Can we call constructor using this and super from a single constructor ? |
->If no constructor is declared in the class compiler creates a default constructor during the time of compilation.
->By default constructor returns the instance of the class. But we cannot do that explicitly.
-> constructor can be overloaded as in the first example we have used both parameterized as well as default constructor .
-> We can use private access specifier to make a constructor private. If we make a constructor private the same cannot be accesses from out side the class.
use:-
When we don't want to create an object of the class out side the class. Used in singleton Design pattern
use:-
When we don't want to create an object of the class out side the class. Used in singleton Design pattern
No comments:
Post a Comment