0% found this document useful (0 votes)
5 views

Constructor

Uploaded by

rockps12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Constructor

Uploaded by

rockps12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Constructor

• It is a special type of method which is used to initialize the object.


• Every time an object is created using the new() keyword, at least one
constructor is called.
• In Java, a constructor is a block of codes similar to the method. It is
called when an instance of the class is created. At the time of calling
constructor, memory for the object is allocated in the memory.

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary
to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't
have any.
Rules for creating Java constructor
• There are two rules defined for the constructor.
• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized
Default
constructor

• If you do not implement any


constructor in your class, Java
compiler inserts a default
constructor into 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.
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
class Demo
{
public Demo()
{
System.out.println("This is a no argument constructor");
}
public static void main(String args[]) {
Demo d1= new Demo();
}
}
Parameterized constructor public class Employee {

int empId;
• Constructor with String empName;

arguments(or you can //parameterized constructor with two parameters


say parameters) is Employee(int id, String name){
this.empId = id;
known as Parameterized this.empName = name;
constructor. }
void info(){
System.out.println("Id: "+empId+" Name: "+empName);
}

public static void main(String args[]){


Employee obj1 = new Employee(10245,“AAA");
Employee obj2 = new Employee(92232,“BBB");
obj1.info();
obj2.info();
}
}
What if you implement only parameterized
constructor in class ?
class Example3
{
private int var;
public Example3(int num)
{
var=num;
}
public int getValue()
{
return var;
}
public static void main(String args[])
{
Example3 myobj = new Example3();
System.out.println("value of var is: "+myobj.getValue());
}
}
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.
In this example, we are creating the no-arg constructor in the Bike class. It will be
invoked at the time of object creation.

Java Default Constructor


A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
1.<class_name>(){}

Example of default constructor


1.//Java Program to create and call a default constructor
2.class Bike1{
3.//creating a default constructor
4.Bike1(){System.out.println("Bike is created");}
5.//main method
6.public static void main(String args[]){
7.//calling a default constructor
8.Bike1 b=new Bike1();
9.}
10.}
What is the purpose of a default constructor?
The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.
Example of default constructor that displays the default values
1.//Let us see another example of default constructor
2.//which displays the default values
3.class Student3{
4.int id;
5.String name;
6.//method to display the value of id and name
7.void display(){System.out.println(id+" "+name);}
8.
9.public static void main(String args[]){
10.//creating objects
11.Student3 s1=new Student3();
12.Student3 s2=new Student3();
13.//displaying values of the object
14.s1.display();
15.s2.display();
16.}
17.}
Output:
0 null 0 null
Explanation:In the above class,you are not creating any constructor so compiler provides you a default constructor.
Here 0 and null values are provided by default constructor.
How many ways to overload Constructors?
There are three ways to overload the constructor and let’s see
the constructor overloading program in java.
1. by changing the number of arguments/parameters.
2. by changing the data type of arguments.
3. The order of the parameters of methods.
1. By changing the number of arguments
We can create multiple constructors of a class, but the number of arguments/parameters should be different.
Let’s create two constructors in class with different numbers of parameters/arguments
class Person
{
Person(String name)
{
System.out.println("Name of person = "+name);
}
Person(String name, String voterId)
{
System.out.println("Name of person = "+name );
System.out.println("Voter ID of " +name+ " = "+ voterId);
}
public static void main (String [] args)
{
// If user has not voter ID then just print name.
Person person1 = new Person("Ravi");
// If user has voter ID then print name and voter Id
Person person2 = new Person("Ram", "12345678");
}
}
2. By changing the type of arguments
We can create multiple constructors with the same number of arguments, but the type of
argument/parameters should be different.
class Person
{
Person(String name, String city)
{
System.out.println("Name of person = "+name);
System.out.println("Name of City = "+city);
}
Person(String name, int voterId)
{
System.out.println("Name of person = "+name );
System.out.println("Voter ID of " +name+ " = "+ voterId);
}
public static void main (String [] args)
{
// If user has city and name then perform different action
Person person1 = new Person("Ravi", "YNR");
// If user has voter ID then print name and voter Id
Person person2 = new Person("Ram", 12345678);
}
3. The Order of the parameters of the methods
You can overload the constructor by changing the order of the parameter. But it is only possible if
both constructors have more than one parameter.
class Person
{
Person(String name, int PAN)
{
System.out.println("Name of person = "+name);
System.out.println("PAN card details = "+PAN);
}
Person(int voterId, String name)
{
System.out.println("Name of person = "+name );
System.out.println("Voter ID of " +name+ " = "+ voterId);
}
public static void main (String [] args)
{
// If user has name and PAN card details then perform some action
Person person1 = new Person("Ravi", 0001112);
// If user has name and voter ID then perform some action
Person person2 = new Person(12345678, "Ram");
}
}
Constructor Chaining

• When A constructor calls another


constructor of same class then this is
called constructor chaining.

You might also like