Constructors:-
Constructor is a special type of method that is used to initialize the object.
Constructor is invoked at the time of object creation.
Advantages of constructors:
1. A constructor eliminates placing the default values.
2. A constructor eliminates calling the normal method implicitly.
- A constructor is similar to a method that is used to initialize the instance variables.
- A constructor's name and class name should be same.
- A constructor may or may not have parameters.
- A constructor without any parameters called default constructor (or) zero parameterized constructor.
- A constructor with one (or) more parameters is called parameterizsed constructor.
- A constructor is called only once at the time of creating the objects.
- At the time of creating an object,we pass parameters to the parameterized constructor.
- A constructor doesn't return any value,not even 'void'.
Difference b/w default constructor and parameterized constructor :
Default constructor is used to initialize every object with same
data.But parametorized constructor is used to initialize each object
with different data.
Difference between Constructor and method
Constructor
|
Method
|
Constructor is used to initialize the state of an object.
|
Method is used to expose behaviour of an object.
|
Constructor must not have return type.
|
Method must have return type.
|
Constructor is invoked implicitly.
|
Method is invoked explicitly.
|
The java compiler provides a default constructor if you don't have any constructor.
|
Method is not provided by compiler in any case.
|
Constructor name must be same as the class name.
|
Method name may or may not be same as class name.
|
e: If there is no constructor in a class, compiler automatically creates a default constructor.
- class Student{
- int id;
- String name;
- void display()
- {
- System.out.println(id+" "+name);
- }
- public static void main(String args[]){
- Student s1=new Student();
- Student s2=new Student();
- s1.display();
- s2.display();
- }
- }
Parameterized constructor
- class Student{
- int id;
- String name;
- Student(int i,String n)
- {
- id = i;
- name = n;
- }
- void display()
- {
- System.out.println(id+" "+name);
- }
- public static void main(String args[])
- {
- Student s1 = new Student(111,"aaa");
- Student s2 = new Student(222,"bbb");
- s1.display();
- s2.display();
- }
- }
what is Constructor overloading:-
Writing two (or) more constructors with the same name but with difference in parameters is called constructor overloading.
Constructor returns a value but, what ?
No.The
default return type of a constructor is the object of the same class.
You cannot make a constructor explicitly return any other data type.
what is the purpose of constructor?
The purpose of the constructor is to instantiate the class
Note:-
If
no constructor is written by the programmer then the JAVA compiler
internally adds default constructor with default values in the class.
Constructor Overloading
- Constructor is a special method that is used to initialize an object.
- Every class has a constructor,if we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class.
- A constructor does not have any return type
- Constructor in Java can not be abstract, static, final or synchronized
- Each time a new object is created at least one constructor will be invoked
Constructor overloading
- Aconstructor can also be overloaded
- Constructor overloading you have multiple constructor with different signature but only difference is that Constructor doesn't have return type in Java.
Example of constructor overloading
- class Student{
- int id;
- String name;
- int age;
- Student(int i,String n){
- id = i;
- name = n;
- }
- Student(int i,String n,int a){
- id = i;
- name = n;
- age=a;
- }
- void display(){System.out.println(id+" "+name+" "+age);}
- public static void main(String args[]){
- Student s1 = new Student(111,"aaa");
- Student s2 = new Student(222,"bbb",25);
- s1.display();
- s2.display();
- }
- }
class Cricketer
{
String name;
String team;
int age;
Cricketer ()
{
name ="";
team ="";
age = 0;
}
Cricketer(String n, String t, int a)
{
name = n;
team = t;
age = a;
}
Cricketer (Cricketer ct)
{
name = ct.name;
team = ct.team;
age = ct.age;
}
public String toString()
{
return "this is " + name + " of "+team;
}
}
Class test:
{
public static void main (String[] args)
{
Cricketer c1 = new Cricketer();
Cricketer c2 = new Cricketer(“Dhoni", "India", 32);
Cricketer c3 = new Cricketer(c2 );
System.out.println(c2);
System.out.println(c3);
c1.name = "Virat";
c1.team= "India";
c1.age = 32;
System .out. print in (c1);
}
}
this is Dhoni of india
this is Dhoni of india
this is virat of india
No comments:
Post a Comment