OOPS in JAVA 6
Constructors 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. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor:
Constructor name must be same as its class name.
Constructor must not have any explicit return type.
Constructrs can not be abstract, static, final or synchronized.
Example:
class Car
{
String name ;
String model;
Car( ) //Constructor
{
name ="";
model="";
}
}
Two types of Constructor:
- Default constructor (no-arg constructor) : Default constructor provides the default values to the object like 0, null etc.
- Parameterized constructor : Default constructor provides the default values to the object like 0, null etc.
Each time a new object is created at least one constructor will be invoked.
Car c = new Car() //Default constructor invoked
Car c = new Car(name); //Parameterized constructor invoked
NOTE : If there is no constructor in a class, compiler automatically creates a default constructor.
Constructors Overloading
Like methods, a constructor can also be overloaded. Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters.
Why do we Overload constructors ?
Constuctor overloading is done to construct object in different ways.
Example:
class Cricketer
{
String name;
String team;
int age;
Cricketer () //default constructor.
{
name ="";
team ="";
age = 0;
}
Cricketer(String n, String t, int a) //constructor overloaded
{
name = n;
team = t;
age = a;
}
Cricketer (Cricketer ckt) //constructor similar to copy constructor of c++
{
name = ckt.name;
team = ckt.team;
age = ckt.age;
}
public String toString()
{
return "This is " + name + " of "+team;
}
}
public class test
{
public static void main (String[] args)
{
Cricketer c1 = new Cricketer();
Cricketer c2 = new Cricketer("Sachin", "India", 43);
Cricketer c3 = new Cricketer(c2 );
c1.name = "Virat";
c1.team= "India";
c1.age = 27;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
Output:
This is Virat of India
This is Sachin of India
This is Sachin of India
Difference between constructor and method in java
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. |
Object is a physical entity. | Class is a logical entity. |
Constructor is invoked implicitly. | Method is invoked explicitly. |
Object is created many times as per requirement. | Class is declared once. |
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. |
Q. What is constructor chaining in Java??
NOTE : Constructor chaining is a phenomena of calling one constructor from another constructor of same class. Since constructor can only be called from another constructor in Java, constructor chaining is used for this purpose.
class Test
{
Test()
{
this(10);
}
Test(int x)
{
System.out.println("x="+x);
}
}
Output:
x=10
Q. Does constructor return any value??
Ans:Yes, Constructor return current class instance (You cannot use return type yet it returns a value).
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )