Saturday 21 March 2015

Constructor && Method Overloading

CONSTRUCTOR  ::

Generally we can initiate the variables by two methods either 1) by using DOT operator or 2) by making a method and then call it.

But java or some other language supports a special method called constructor.
Constructor enables the objects to initiate itself when it is created..

For Example take a simple rectangle area calculation program

Class Rectangle
{
int length,breadth;
Rectangle(int x,int y )
{
length=x;
breadth=y;
}
int area( )
{
int a;
a=length * breadth; 
return(a );
System.out.println("Area of rectangle is:"+a );
}

Class calarea

{
public static void main( string args[])
{
Rectangle rect1= new Rectangle(10,15);
 rect1.area( );
}
}

In this program we initialise the object by using constructor Rectangle(int x,int y)..

Here this constructor is known as Parameterized Constructor that is because at the time of object intantiation,the constructor is explicitly invoked by passing certain arguments.
But if we want the constructor to automatically initialize the object variables with some default values at the same time of object intantiation ??? Then Default Constructors are used..

Use of default and parameterized constructor with example-

Class Rectangle
{
int length,breadth;
//Default Constructor 
Rectangle( )
{
length=0;
breadth=0;
}
//Parameterized Constructor
Rectangle(int x,int y )
{
length=x;
breadth=y;
}
int area( )
{
int a;
a=length * breadth; 
return(a );
System.out.println("Area of rectangle is:"+a );
}

Class calarea

{
public static void main( string args[])
{
Rectangle rect1= new Rectangle();
Rectangle rect2= new Rectangle(10,15);
rect1.area( );
 rect2.area( );
} }

Ans Will be....0 and 150


Properties of Constructor -


What can not?
1. "A constructor cannot be abstractstaticfinalnative, strictfp, or synchronized".
2. Interface cannot have constructor.
3. Constructor cannot return a value.{return object}


What can?
1. A constructor can be private.
2. Abstract class can have constructor.
3. A constructor can be overloaded.

4.Constructors have the same name as the class name.

Now Comes on the METHOD OVERLOADING

Method Overloading definition can be comprised with two points

1. Same method name
2. Different argument list 

Method Overloading is used when objects are required to perform the same task with different input parameter..We can understand method overloading more clearly bu refering the above example


Class Rectangle

{
int length,breadth;
//Default Constructor 
Rectangle( )
{
length=0;
breadth=0;
}
//Parameterized Constructor
Rectangle(int x,int y )
{
length=x;
breadth=y;
}
int area( )
{
int a;
a=length * breadth; 
return(a );
System.out.println("Area of rectangle is:"+a );
}

Class calarea

{
public static void main( string args[])
{
Rectangle rect1= new Rectangle();
Rectangle rect2= new Rectangle(10,15);
 rect1.area( );
rect2.area( );
}
 }

In this program we can see that same method "Reactangle" is using with two different parameter list so simply we can say that the constructor method is being overloaded...
First the method name will be matched after that it will match the number and type of the parameters and according to this call the function.

No comments:

Post a Comment