Constructor in Java

  • Constructor java

Constructor in Java

Definition :
A Constructor is a block code , Special types of method that is called when an instance of an object is created.

Difference between constructor and method :

  • Constructor does not have return type
  • The name of the constructor will be same as the name of the class.
  • In class, the unlike method and constructor are not considered
  • A constructor is known as , When a new instance of object is created.

Basic Format for Coding Constructor :-

Public Classname ( parameter_list) [ throws exception ]
{
Statement
}

Rules to creating a java constructor :-

1. The constructor name must match the name of the class.
2. constructor must not have no explicit return type value.
3. A default constructor will be automatically created by a computer, when you don’t type a constructor into your class code
4. The default constructor is ALWAYS a no-arg constructor.
5. You cannot make a instance method, untill after the super constructor runs
6. The static variable and method can be accessed as part of the call to this () or super().8. Interface are not part of an object inheritance tree. Because, its not have constructor.
9. The only way a constructor can be invoked it form with in another constructor.

Types of constructor
There are two types of constructor
                                  i. Default constructor
                                  ii. Parameterized Constructor

java-constructor

 

Default Constructor 

A constructor does not have parameter is called Default Constructor .

Syntax :-

<class_name>(){}

Eample Default Constructor :

class Bike1

{
Bike1(){System.out.println(“Bike is created”);

}
public static void main(String args[])

{
Bike1 b=new Bike1();
}
}

Output:

Bike is created

Rule

 –>If there is no constructor in a class, compiler automatically creates a default constructor.

Constructor Java

Example of default constructor that displays the default values

class Student3
{
int id;
String name;
void display()
{
System.out.println(id+” “+name);
}
public static void main(String args[])
{
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}

Output:

0 null
0 null

Parameterized Constructor :

A constructor have parameter is called Parameterized Constructor.

Example of Parameterized Constructor :

class Student4
{
int id;
String name;
Student4(int i,String n)
{
id = i;
name = n;
}
void display(){System.out.println(id+” “+name);
}
public static void main(String args[])
{
Student4 s1 = new Student4(111,”Karan”);
Student4 s2 = new Student4(222,”Aryan”);
s1.display();
s2.display();
}
}

Output:

111 Karan
222 Aryan

Constructor Overloading in Java
Constructor Overloading is nothing but a class can have a any number of construtor that differ in parameter list and it can differeftiate these constructor by taking into the account the number of parameter in the list and its type.

Example of Constructor Overloading

class Student5
{
int id;
String name;
int age;
Student5(int i,String n)
{
id = i;
name = n;
}
Student5(int i,String n,int a)
{
id = i;
name = n;
age=a;
}
void display()
{
System.out.println(id+” “+name+” “+age);
}
public static void main(String args[])
{
Student5 s1 = new Student5(111,”Karan”);
Student5 s2 = new Student5(222,”Aryan”,25);
s1.display();
s2.display();
}
}

Output:

111 Karan 0
222 Aryan 25