Tuesday, 7 February 2017

Constructor in Java

Constructor in Java: -

Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation.

Rules for creating java constructor: -

There are basically two rules defined for the constructor.
1)      Constructor name must be same as its class name
2)      Constructor must have no explicit return type

Types of java constructors: -

There are two types of constructors:
1)      Default constructor (no-argument)
2)      Parameterized constructor

Java Default Constructor: -

A constructor that have no parameter is known as default constructor.
Syntax of default constructor: -
<class_name>()
{
}

Example of default constructor: -

class B
{
    B()
            {
                        System.out.println("Bike is Running");
            }
public static void main(String args[])
            {
                        B b=new B();
             }
}

Output:

Bike is Running

Example of default constructor that displays the default values

class stud
{
int id;
String n;
void dis()
            {
                        System.out.println(id + "  " + n);
            }
public static void main(String args[])
            {
                        stud s1=new stud ();
                        stud s2=new stud ();
                        s1.dis();
                        s2.dis();
            }
}

Output: -

0 null
0 null

Java parameterized constructor

A constructor that have parameters is known as parameterized constructor.

Use of parameterized constructor

Parameterized constructor is used to provide different values to the distinct objects.

Example of parameterized constructor

class stud
{
int id;
String name;
stud()
{
id = i;
name = n;
}
stud()
            {
                        System.out.println(id+" "+name);
            }
public static void main(String args[])
            {
stud s1 = new stud(311,"Safal");
stud s2 = new stud(312,"Sahil");
                        s1.display();
                        s2.display();
}
}

Output:

111 Karan
222 Aryan

Simple Example of Constructor OverLoading

class Stud
{
Stud()
            {
                          System.out.println("Hello");
            }
Stud(int a)
            {
                        System.out.println(a);
            }
public static void main(String args[])
            {
                        Stud s1 = new Stud();
                        Stud s2 = new Stud(5);
             }
}

Output:

Hello
5

Difference between Constructor and Method in java

Method Constructor
Method is used to expose behaviour of an object. Constructor is used to initialize the state of an object.
Method must have return type. Constructor must not have return type.
Method is invoked explicitly. Constructor is invoked implicitly.
Method is not provided by compiler in any case. The java compiler provides a default constructor if you don't have any constructor.
Method name may or may not be same as class name. Constructor name must be same as the class name.
 

No comments:

Post a Comment