Constructor
Constructor is a special method that gets automatically call only when object is initialized by "new" keyword that is one instance (dynamic object) created.
It must be declared in default access or in public visibility mode so that it can be automatically called by instance.
In java when a method is declared inside the class whose name match with class name then it is termed as constructor.
The constructor may have parameter but does not not have return type.
Note: The purpose of the constructor is to initialize its data members of the invoking instance.
Constructor is a special method that gets automatically call only when object is initialized by "new" keyword that is one instance (dynamic object) created.
It must be declared in default access or in public visibility mode so that it can be automatically called by instance.
In java when a method is declared inside the class whose name match with class name then it is termed as constructor.
The constructor may have parameter but does not not have return type.
Note: The purpose of the constructor is to initialize its data members of the invoking instance.
Syntax of Constructor
Always keep in mind two things:
- Constructor name must be same as its class name.
- Constructor must have no explicit return type but it may access modifier
Access modifier className(Type of parameter); // Access modifier and Parameter is not compulsory
example:
public Demo(int a)
Types of Constructor
- Default Constructor
- Parameterized Constructor
- Copy Constructor
1.Default Constructor - When there is no Parameter in a constructor declaration then it is termed as default constructor. If a class has no constructor compiler always call default constructor.
Syntax :
class Demo
{
Demo() //default constructor
{
//body of constructor
}
}
Example :
class Demo1
{
public Demo1()
{
System.out.println("Default Constructor of Demo1");
}
public static void main(String args[])
{
Demo1 obj=new Demo1(); // default constructor calling and object creation
}
}
output
Default Constructor of Demo1
Example of default constructor that display the default values:
Example of default constructor that displays the default values:
class Demo2
{
int id;
String name;
void display()
{
System.out.println(id);
System.out.println(name);
}
public static void main(String args[])
{
Demo2 obj=new Demo2(); //default constructor calling & object creation
obj.display();
}
}
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.
2. Parameterized Constructor:
When parameter is declared in a constructor declaration then it is termed as parameter constructor.
syntax:
class Demo
{
Demo(int x) //parameterized constructor
{
//body of constructor
}
}
class Demo
{
Demo(int x) //parameterized constructor
{
//body of constructor
}
}
Example of parameterized constructor:
In this example, we have created the constructor of Demo3 class that have two parameters. We can have any number of parameters in the constructor.
class Demo3
{
int id;
String name;
public Demo3(int i,String n) //Parameterized Constructor
{
id = i;
name = n;
}
void display()
{
System.out.println(id+": "+name);
}
public static void main(String args[])
{
Demo3 d1 = new Demo3(101,"Prabhakar"); //parameterized costructor calling and object creation
Demo3 d2 = new Demo3(102,"Akash"); //parameterized costructor calling and object creation
d1.display();
d2.display();
}
}
Output:
101: Prabhakar
102: Akash
101: Prabhakar
102: Akash
Constructor Overloading:
More than one constructor can be declared inside the class is called constructor overloading but number of parameter should be different or type of parameter should be different like method overloading.
class Demo4
{
int id;
String name;
int age;
Demo4(int i,String n) // Parameter Constructor-1
{
id = i;
name = n;
}
Demo4(int i,String n,int a) // Parameter Constructor-2
{
id = i;
name = n;
age=a;
}
void display()
{
System.out.println(id+", "+name+", "+age);
}
public static void main(String args[])
{
Demo4 d1 = new Demo4(101,"Prabhakar");
Demo4 d2 = new Demo4(102,"Akash",25);
d1.display();
d2.display();
}
}
Output:
101, Prabhakar, 0
102, Akash, 25
101, Prabhakar, 0
102, Akash, 25
3. Copy Constructor :
It is also a parameter constructor and contains one of the parameter as an object of same class type (The values of parameter object are assigned to the data members invoking object in the processing of the constructor), then such constructor declaration is termed as copy constructor.
NOTE: One object can call only one constructor according to the arguments given to its instance ().
Example:
class Demo5
{
int id;
String name;
Demo5(int i,String n) // parameterized constructor
{
id = i;
name = n;
}
Demo5 (Demo5 d) //copy constructor
{
id = d.id;
name =d.name;
}
void display()
{
System.out.println(id+": "+name);
}
public static void main(String args[])
{
Demo5 d1 = new Demo5(101,"Prabhakar");
Demo5 d2 = new Demo5(d1); //Copy constructor calling
d1.display();
d2.display();
}
}
Output:
101: Prabhakar
101: Prabhakar
101: Prabhakar
101: Prabhakar
Important Questions :
Que) Does constructor return any value?
Ans: Yes,that is current class instance (You cannot use return type yet it returns a value).
Ans: Yes,that is current class instance (You cannot use return type yet it returns a value).
Que) Can constructor perform other tasks instead of initialization?
Ans: Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method.
Ans: Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method.
Que) What is the difference between constructor and method ?
Ans: There are many differences between constructors and methods. They are given below.
Ans: There are many differences between constructors and methods. They are given below.
1. Constructor is used to initialize the state of an object.
and Method is used to expose behaviour of an object.
and Method is used to expose behaviour of an object.
2. Constructor must not have return type.
and Method must have return type.
and Method must have return type.
3. Constructor is invoked implicitly.
and Method is invoked explicitly.
and Method is invoked explicitly.
4. The java compiler provides a default constructor if you don't have any constructor.
and Method is not provided by compiler in any case.
and Method is not provided by compiler in any case.
5. Constructor name must be same as the class name.
and Method name may or may not be same as class name.
and Method name may or may not be same as class name.
0 comments:
Post a Comment