15) Constructors
Last updated
Last updated
What happens when you open a new bank account? Depending on the services your bank provides, you may be assigned a new bank account number, provided with a check- book, and given access to a new online account the bank has created for you. These details are created and returned to you as part of setting up your new bank account.
Constructor is a special method which is used to construct an object or create an object for a class.
Constructor does not return any value or does not have any Return type.
Whenever an object of class is created using new operator constructor will be executed.
We can use constructor to create object, without constructor we cannot create Object.
Rules to develop constructor:
Constructor named should be exactly same as Class name.
Constructor should not have return type, should always be non-static should not return any value.
Constructor is also called as Non-static initializer, which is used to initialize the object of a class.
No static and return type allowed.
Constructor should be there for every class.
If the programmer has not developed a constructor for class then there will be default constructor in that class.
The constructor developed by the compiler at compile-time is called as Default constructor. If a programmer has not developed a constructor for class then compiler will develop the default constructor.
Methods
Constructor
Method can have any name; it can have a class name also.
Constructor name Should be similar to the Class name.
Method must have return type at least void
Constructor should have Return type not even void
Method can be static or non-static.
Constructor should always be non-static
Method may or may not return value.
Constructor cannot return value.
Java does not provide Default Method
Java provides Default Constructor.
Method cannot be used for Object creation.
Constructor will be used for object creation using new operator.
New operator will allocate the new address space randomly in the heap memory.
All the Non-Static members of class will be loaded onto the address space with default values.
Constructor body will be executed on Stack memory like a method.
Object address will be assigned to Reference variables.
If the programmer needs to initialize the instance variable then programmer can perform the initialization in Constructor.
If the programmer wants to perform some start up activities whenever an object is created for the class, then programmer will reuse the Constructor body and all the startup code will be developed inside the Constructor.
Parameterized Constructor: The Constructor which is developed with some arguments. Example: A(int I)
No-Arguments Constructor: The Constructor which is developed without arguments. Example: A()
** Default Constructor falls under No-Arguments Constructor.
To create the object for a class we should utilize available Constructor of that class.
If programmer develops a Constructor for class then java does not provide a default Constructor.
Developing multiple Constructors for a class with the variation in the Data-type, members and position of arguments is called Constructor overloading.
Example:
Output:
Main Starts
Running Constructor
Main Ends
Example:
Output:
Main Starts
Running Demo1 constructor
------------
Running Demo1 constructor
Main Ends
Example:
Output:
Main Starts
Main Ends
Example:
Output:
Main Starts
Main Ends
-----------
Example:
Output:
Main Starts
Running Constructor
Value of stdId is :0
--------------------
Running Constructor
Value of stdId is :0
Main Ends
Example: Parametrized Constructor
Output:
Main Starts
Employee id is:10
Employee id is:20
Main Ends
Example:
Output:
Main Starts
1st student id is:1
1st student name is:JSM
-------------------------
1st student id is:1
1st student name is:SMP
Main Ends
Example:
Output:
Area is:16.610599999999998
Area is:84.90560000000002
Example:
Output:
From A()
From A(int a)
From A(double d)
From A(inti,double d)
From A(inti,double d)
Example:
Output:
1st student id is:10
1st student Name is:JSM
1st student age is:90
1st student id is:20
1st student Name is:SMP
Output: It will throw a compilation error. The reason is, the statement Example3 myobj = new Example3() is invoking a default constructor which we don’t have in our program. When you don’t implement any constructor in your class, compiler inserts the default constructor into your code, however when you implement any constructor (in above example I have implemented parameterized constructor with int parameter), then you don’t receive the default constructor by compiler into your code.
If we remove the parameterized constructor from the above code then the program would run fine, because then compiler would insert the default constructor into your code.
Can we Override constructor?
NO, because constructor will not be inherited to sub class but for method overriding inheritance is mandatory.
Can we override main method?
NO, because main is static method, static method will not be inherited to sub class and for method overriding inheritance is mandatory.
Can we Override Static members?
NO, because static methods will not be inherited to sub class but for method overriding inheritance is mandatory.
Q. What is constructor chaining in Java?
Constructor chaining is a phenomenon of calling one constructor from another constructor of same class. Since constructor can only be called from another constructor in Java, constructor chaining is used for this purpose.
Java will treat it as another method, not a constructor, which also implies that it won’t be called implicitly when you create an object of its class:
How do you execute such a method?
By calling it explicitly, as in the following code (modified code is in bold):
What happens if you add another constructor to the class Employee, as in the following example?
Java defines a default constructor if and only if you don’t define a constructor. If a class doesn’t define a constructor, the compiler will add a default, no-argument constructor to the class. But if you modify the class later by adding a constructor to it, the Java compiler will remove the default, no- argument constructor that it initially added to the class.