Tuesday, September 6, 2022

Constructor in Java :

  1. constructor in java is a special type of method that is used to initialize a newly created object before it is used.
  2. constructor is invoked at the time of object creation. It constructs the value for the object so that is why it is known as constructor.

Rules for creating constructor:

  1. Constructor name must be the same as its class name.
  2. constructor must not have any return type.
  3. We must create an object for the class then only the constructor invoked.

Types of constructors in Java:

  1. Default constructor.
  2. Parameterized constructor.
  3. Copy constructor

Default constructor :

Default constructor is not parameterized and it provides the default values to the object depending on the data type. Syntax:

1
2
3
4
5
6
7
8
9
10
11
public class <strong>class_name</strong>
{
    public <strong>class_name()</strong>     //default constructor
    {
        //statements;
    }
    public static void main(String[] args)
    {
        <strong>class_name  object=new class_name();</strong>
    }
}

Example :

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Myclass
{
    int x,y,sum;
    public Myclass()
   {
        sum=x+y;
    }
    public static void main(String[] args)
    {
        Myclass ob=new Myclass();
        System.out.println("sum is "+ob.sum);
    }
}

 Output:

Sum is 0.

Parameterized constructor :

  1. In a parameterized constructor we can pass arguments/parameters .
  2. They are used to provide different values to the distinct objects.

syntax :

1
2
3
4
5
6
7
8
9
10
11
public class class_name
{
    public class_name(data_type1 variable_name1,data_type2 variable_name2,................)  //parameterized constructor
    {
        //statements;
    }
    public static void main(String[] args)
    {
        class_name  object=new class_name(value1,value2,..............);
    }
}

Example : 

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Myclass
{
    int x,y,sum;
    public  Myclass(int x,int y)
   {
        sum=x+y;
    }
    public static void main(String[] args)
    {
        Myclass ob=new Myclass(2,3);
        System.out.println("sum is "+ob.sum);
    }
}

 output :

sum is 5

Copy constructor :

  1. Copy constructor is a special type of constructor,which is used to copy objects from other objects of the same class but different addresses.
  2. Copy constructor is used to construct an object by copying the state of another object of the same class.
  3. We can copy default or parameterized constructors.

Syntax :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class class_name
{
    public class_name()     //default  or parameterized constructor.
    {
        //statements;
    }
    public class_name(class_name object)     //copy constructor.
{
        //statements;
    }
    public static void main(String[] args)
    {
        class_name  object1=new class_name();
        class_name  object2=new class_name(object1);
 
    }
}

 

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Myclass
{
    String name;
    public Myclass(String n)      //parameterized constructor
{
        name=n;
}
public Myclass(Myclass ob)      //copy constructor
{
        name=ob.name;
}
public void display()
{
System.out.println(name);
}
public static void main(String[] args)
{
        Myclass ob1=new Myclass("Programming9");
        Myclass ob2=new Myclass(ob1);
        ob1.display();
        ob2.display();
}
}

 

Output :

Programming9

Programming9

Difference between method and constructor:

  • Constructor is automatically invoked when we create the object for the method and there is no need to call the constructor, but for the method we need to call the method by reference or by value.
  • The name of the constructor should be the same as the class name , but the name of the method does not depend on the class name.

Example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Myclass
{
    int a=0,b=0;
    public  Myclass()   //constructor (name is same as class)
    {
        a=5;
    }
    public void  Method()     //method (name is not dependent on class)
    {
        b=6;
    }
    public static void main(String []args)
    {
        Myclass ob=new Myclass(); //here we created object the constructor is automatically invoke
           ob.Method();   // here method is invoked because we called it by reference
           System.out.println(ob.a);
           System.out.println(ob.b);
     }
}

Output:

5

6

Constructor overloading

  • If we have more than one constructor in a class then it is known as constructor overloading.
  • when the number of parameters passed in one constructor then datatype should be different in another constructor , when data types are same in one constructor then number of parameters passed should be different in other constructor, then only constructor overloading occurs.
  • For each constructor we need to create one object because the constructors are different from each other.

Syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class class_name
{
    class_name()
    {
    }
    class_name(datatype1  variable1,datatype2 variable2,...)
    {
    }
    public static void main(String argos[])
    {
        Class_name ob1=new class_name();
        Class_name ob1=new class_name(value1, value2,....);
        .
        .
        .
        .
         }
}

Example : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Myclass
{
    int c;
    double d;
    String  str;
    public Myclass(int a,int b)
    {
        c=a+b;
    }
    public Myclass(double  a,double b)
    {
        d=a*b;
    }
    public Myclass(String n)
    {
        str=n;
    }
    public static void main(String[] args)
    {
        Myclass ob=new Myclass(2,3);
        Myclass ob1=new Myclass(2.0,3.0);
        Myclass ob2=new Myclass("Done");
        System.out.println(ob.c);
        System.out.println(ob1.d);
        System.out.println(ob2.str);
    }
}

Output:

5

6.0

Done

  • When we have same constructors that mean same number of arguments and same type the we get error .

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Myclass
{
     
    public Myclass()
    {
        System.out.println("constructor 1");
    }
    public Myclass()
    {
         System.out.println("constructor 2");
    }
     
    public static void main(String[] args)
    {
        Myclass ob=new Myclass();
        Myclass ob1=new Myclass();
       
    }
     
}

Output:(error)

Myclass.java:8: error: constructor Myclass() is already defined in class Myclass public Myclass()

No comments:

Post a Comment

techguruspeaks

  Core Java programming Introduction to Java Java Features Writing a Simple Java Program Java Keywords and Comments Variables, Identifiers ...