Constructor
Constructor
com/2013/03/constructors-in-java/
Constructor has same name as the class and looks like this in a java code.
Note that the constructor name matches with the class name and it doesn’t have
a return type.
Output:
Beginners
Types of Constructors
There are three types of constructors: Default, No-arg constructor and
Parameterized.
https://beginnersbook.com/2013/03/constructors-in-java/
Default constructor
If you do not implement any constructor in your class, Java compiler inserts
a default constructorinto your code on your behalf. This constructor is known as
default constructor. You would not find it in your source code(the java file) as it
would be inserted into the code during compilation and exists in .class file. This
process is shown in the diagram below:
If you implement any constructor then you no longer receive a default constructor
from Java compiler.
https://beginnersbook.com/2013/03/constructors-in-java/
no-arg constructor:
Constructor with no arguments is known as no-arg constructor. The signature
is same as default constructor, however body can have any code unlike default
constructor where the body of the constructor is empty.
Although you may see some people claim that that default and no-arg
constructor is same but in fact they are not, even if you write public Demo() { } in
your class Demo it cannot be called default constructor since you have written the
code of it.
Parameterized constructor
Constructor with arguments(or you can say parameters) is known
as Parameterized constructor.
int empId;
String empName;
the object using new keyword then default constructor is invoked, however when
you pass a parameter then parameterized constructor that matches with the
passed parameters list gets invoked.
class Example2
{
private int var;
//default constructor
public Example2() // no argument
{
this.var = 10;
}
//parameterized constructor
public Example2(int num) // parameterized
{
this.var = num;
}
public int getValue()
{
return var;
}
public static void main(String args[])
{
Example2 obj = new Example2();
Example2 obj2 = new Example2(100);
System.out.println("var is: "+obj.getValue());
System.out.println("var is: "+obj2.getValue());
}
}
Output:
var is: 10
var is: 100
}
}
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.
Constructor Chaining
When A constructor calls another constructor of same class then this is called
constructor chaining. Read more about it here.
https://beginnersbook.com/2013/03/constructors-in-java/
Super()
Whenever a child class constructor gets invoked it implicitly invokes the
constructor of parent class. You can also say that the compiler inserts
a super(); statement at the beginning of child class constructor.
class MyParentClass {
MyParentClass(){
System.out.println("MyParentClass Constructor");
}
}
class MyChildClass extends MyParentClass{
MyChildClass() {
System.out.println("MyChildClass Constructor");
}
public static void main(String args[]) {
new MyChildClass();
}
}
Output:
MyParentClass Constructor
MyChildClass Constructor
Constructor Overloading
Constructor overloading is a concept of having more than one constructor with
different parameters list, in such a way so that each constructor performs a
different task.
https://beginnersbook.com/2013/03/constructors-in-java/
StudentData.java
class StudentData
{
private int stuID;
private String stuName;
private int stuAge;
StudentData()
{
//Default constructor
https://beginnersbook.com/2013/03/constructors-in-java/
stuID = 100;
stuName = "New Student";
stuAge = 18;
}
StudentData(int num1, String str, int num2)
{
//Parameterized constructor
stuID = num1;
stuName = str;
stuAge = num2;
}
//Getter and setter methods
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
Constructor Override
a constructor cannot be overridden
A constructor is a special member function of class with the same name as that of class.
class JavaExample{
String web;
JavaExample(String w){
web = w;
}
Output:
Website: BeginnersBook
Website: BeginnersBook
https://beginnersbook.com/2013/03/constructors-in-java/
https://beginnersbook.com/2013/03/constructors-in-java/