Constructors in Java. Types of constructors and constructor overloading.
Constructors in Java:
Definition : It is a special type of method which is used to creating and initializing the objects.
It is called when an instance(object) of the class is created.(instance means object).Every class has a constructor either implicitly or explicitly.
Every time an object is created using the new() keyword, at least one constructor is called.
Rules for creating Java constructor:
1.Constructor name must be the same as its class name.
2.A Constructor must have no explicit return type.
3.A Java constructor cannot be abstract, static, final, and synchronized.
4.constructor can be overloaded. But it cannot be overriden.
Types of constructors:
1.Default Constructor (no-arg)
2.Parameterized Constructor.
3.Copy constructor.
1.Default Constructor:
Default constructor is the constructor which doesn’t take any argument.
It has no parameters. Also known as empty constructor.
If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program.
Syntax :
<class_name>( )
{
}
example:
student()
{
}
Program :
public class student
{
int rno;
String name;
public student() //default constructor
{
System.out.println("student is created");
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
}
public static void main(String args[])
{
//calling a default constructor
student s=new student();
}
}
Output:
student is created
roll no:0
name:null
2. Parameterized Constructor :
A constructor which has a specific number of parameters is called a parameterized constructor.
Constructor takes the one or more than one parameters(arguments).
Why use the parameterized constructor?
The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
syntax:
public classname(parameters)
{
}
Example:
public student( int r,String n)
{
rno=r;
name=n;
}
Program :
public class student
{
int rno;
String name;
public student()
{
}
public student( int r,String n)
{
rno=r;
name=n;
}
public void display()
{
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
}
public static void main(String args[ ])
{
student s=new student();
student s1=new student(10,"xyz");
s1.display();
student s2=new student(11,"abc");
s2.display();
}
}
Output:
roll no :10
name:xyz
roll no :11
name:abc
3.Copy constructor :
it is also type constructor to initialize the values from one object to another object of same class.
Or
It is constructor which is used to copy the data from one object to another object of the same class type.
copy constructor is a special type of constructor that takes same class as argument.
copy constructors are used to create duplicate or cloned objects.
syntax:
public classname(classname object)
{
feilds= object.feilds;
}
Example :
public student(student s)
{
rno= s.rno;
name= s.name;
}
Program :
public class student
{
int rno;
String name;
public student()//default
{
}
public student( int r,String n)//parameter
{
rno=r;
name=n;
}
public student(student s)//copy
{
rno= s.rno;
name= s.name;
}
public void display()
{
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
}
public static void main(String args[ ])
{
student s=new student();
student s1=new student(10,"xyz");
s1.display();
student s2=new student(s1);
s2.display();
}
}
Output:
roll no :10
name:xyz
roll no :10
name:xyz
Constructor overloading :
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists.
They are arranged in a way that each constructor performs a different task.
or
constructor overloading is a mechanism having more than one constrcutor with diffrent parameters in a same (single) class.
Example :
class student
{
public student( int r,String n)
{
rno=r;
name=n;
}
public student (int r ,String n, float a)
{
rno=r;
name=n;
avg=a;
}
public student(student s)
{rno= s.rno;
name= s.name;}
}
Program :
public class student
{
int rno;
String name;
float avg;
public student()
{
System.out.println("student is created:");
}
public student( int r,String n)
{
rno=r;
name=n;
}
public student (int r ,String n, float a)
{
rno=r;
name=n;
avg=a;
}
public void display()
{
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
System.out.println("average:"+avg);
}
public static void main(String args[ ])
{
student s=new student();
student s1=new student(10,"xyz");
s1.display();
student s3=new student(12,"mno",80.05);
s3.display();
}
}
Output:
student created:
roll no :10
name:xyz
average:0.0
roll no :12
name:mno
average:80.05
Private Constructors:
we can create private constructor.
It means by declaring a private constructor, it restricts to create object of that class.
Syntax:
private classname()
{
}
Example :
private student()
{
}
Program :
public class student
{
static int rno;
static String name;
private student()
{
}
public static void getdata(int r,String n)
{
rno=r;
name=n;
}
public static void putdata()
{
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
}
public static void main(String args [])
{
getdata(10,"Omkar");
putdata();
}
}
Output :
roll no :10
name: Omkar
Comments
Post a Comment