Thursday, May 19, 2016

CONSTRUCTOR IN JAVA

In this Article we will discuss about constructor in java with simple examples. After creations of object compulsory we should perform initialization then only that object in a position to provide service to others. 

what is constructor?

A constructor is similar to a method that is used to initialize the instance variables.The purpose of constructor is to initialize the instance variables. That means to initialize the object.

When it Works either before Object creation or after object creation:

At the time of Object creation some piece of code will execute automatically to perform initialization that piece of code is nothing but "constructor".

What If Don't Use constructor in our program see the fallowing example ?

class Student
{
String name;
int rollno;
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.name="mahesh";
s1.rollno=10;
s2.name="pavan";
s2.rollno=11;
s3.name="lucky";
s3.rollno=13;
System.out.println(s1.rollno+"....."+s1.name);
System.out.println(s2.rollno+"....."+s2.name);
System.out.println(s3.rollno+"....."+s3.name);
}
}

To overcome this type of burden by using constructor  rewrite preceding program as fallowing example.

class Student
{
String name;
int rollno;
Student(int rollno,String name)
{
this.name = name;
this.rollno = rollno;
}
public static void main(String args[])
{
Student s1 = new Student(10,"mahesh");
Student s2 = new Student(11,"pavan");
Student s3=new Student(12,"lucky");
System.out.println(s1.rollno+"-----"+s1.name);
System.out.println(s2.rollno+"-----"+s2.name);
System.out.println(s3.rollno+"-----"+s3.name);
}

}

Output:








Rules for creating java constructor


  • The constructor's name and class name must be same.

Example : If we take Person class we can write constructor as:


Person()
{
}
  • A constructor may have or may not have parameters. Here parameters are variables to receive data from outside into the constructor.If constructor does not have any parameters,it is called 'default constructor'. If constructor has 1 or more parameters,it is called 'parameterized constructor'.
Example:                      

Person()
{                 //default constructor
}
Person(String s,int i)
{
 }                   //parameterized constructor with two parameters

  • A constructor does not return any value, not even 'void'.
  • A constructor is automatically called and executed at the time of creating object.while creating an object,if nothing is passed to the object,the default constructor is called and executed.If some values are passed to the object, then the parametrized constructor is called.
For example,if we create object like as:
                      
Person p=new Person();  //default constructor is called here.
           
Person p=new Person("lucky",28);     //parameterized constructor is called here
  •  A constructor is called and executed only once per object.This means when we create an object,the constructor is called.
Types of Constructors:

Default Constructor :

If a constructor has no parameters it is known as default constructor

// Default constructor Example

class Person{
//instance variables
private String name;
private int age;
Person()                      //default constructor
{
name="lucky";
age=28;
}
void talk()         //method
{
System.out.println("this is"+name);
System.out.println("my age is"+age);
}
class Dconst
{
public static void main(String args[])
{
Person p=new Person();
//call the talk method
lucky.talk();

output:

javac Dconst.java
java Dconst

this is lucky
my age is 28

Parameterized constructor:

A constructor that have parameters is known as parameterized constructor

Example:

class person{
//instance variables
private String name;
private int age;
//default constructor
person()
{
name="lucky";
age=28;
}

//parameter constructor
Person(String s  int i)
{
name=s;
age=i;
}
void talk()


System.out.println("this is"+name);
System.out.println("my age is"+age);
}
}
class Test{
public static void main(String args[])
{
Person p=new Person();
lucky.talk();
Person p1=new Person("rama",30);
p1.talk();
}
}

Output:

this is lucky
my age is 28
this is rama
my age is 30

Overload Constructor:

A class can contain more than one constructor with different arguments. This type of constructor are called " overload constructor".

Example:

class Demo
{
Demo()
{
this(20);
System.out.println("No-arg constructor");
}
Demo(int i)
{
this(20.5);
System.out.println("int-arg");
}
Demo(double d)
{
System.out.println("double-arg");
}
public static void main(String arg[])
{
Demo d1 = new Demo();
Demo d2 = new Demo(20);
}


Output:






Note:  Inheritance concept not applicable for constructor and hence Override concept also not applicable.


High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...