Sunday 31 July 2011

Constructor Overloading


CONSTRUCTOR OVERLOADING:
Constructors are chunks of code used to initialize a new object. As with overloaded methods, if you want to provide more than one constructor in a class, each one must have a different signature. Constructors always have the name of the class and no return type.

Example:
public class Movie
{
private String title;
private String rating = “PG”;
public Movie()
{
title = “Last Action- -”;
}
public Movie (String Ntitle)
{
title = Ntitle;
}
}

Calls no argument Constructor ------------------------------- Movie mov1 = new Movie();

Calls Single argument Constructor ---------------------------  Movie mov2 = new Movie("Gone with the wind);


Class can have any number of constructors as long as they have different signatures.

Method Overloading

METHOD OVERLOADING:
In java, it is possible to create methods that have the same name, provided they have different signatures. A signature is formed from its name, together with the number and types of its arguments. The method in return type is not considered a part of the method signature. Method overloading is used when objects are required to perform a similar task but by using different input parameters. When we call a method in an object, java matches up the method name first, and then the number and type of parameters, to decide which method to execute. This process is known as Polymorphism.

Public class Movie
{
float price;
public void setprice()
{
price=3.50;
}
public void setprice (float newprice)
{
price=newprice;
}
}
Movie mov1=new Movie();
mov1.setprice();
mov1.setprice(3.25);

As you can see setprice() is overloaded two times. The first version takes no argument and sets the price to 3.50 and second constructor takes single argument whic sets the price to 3.25.

When an overloaded method is called, java looks for a match between the argument used to call the method and the method's parameters. However, this match need not be exact. In some cases java's automatic type conversion takes the important role in overloaded resolution.

Example:
public class Movie
{
float price;
public void disp()
{
System.out.println(“No parameter”);
}
public void disp(int x, int y)
{
System.out.println(“x = ” + x + “y = “ +y);
}
public void disp(double x)
{
System.out.println(“x =” +x);
}
}
class overload
{
public static void main ( String args[])
{
Overload ob = new overload();
int i = 100;
ob.disp(); //invokes no argument constructor
ob.disp(10,20); //invokes disp(int,int)
ob.disp(i); //invokes disp (double)
ob.disp(100.50) //invokes disp(double)
}
}

Class Movie does not define disp(int). When disp(int) is called from main(), when no matching method is found, java automatically converts an integer into a double. Java uses its automatic type conversion only if no exact match is found.
Java's standard class library has abs() method. This method returns absolute value of all type of numeric types e.g. Int double, float. This method is overloaded in Math class. Whereas in 'C' language there are three methods – abs(),fabs(),labs()- to return absolute of int, float and long values respectively, the java.awt.Graphics class has six different drawImage methods.

As far as java is concerned, these are entirely different methods. The duplicate names are really just a convenience for the programmer.



Constructors


CONSTRUCTORS:

When the object is created, Java performs default initialization, char variables are set to '\u0000', byte, short, int, long to zero, boolean to false, float to 0.0

All objects that are created must be given initial values. We can do it,

  1. By assigning the value to instance variable using dot operator.
  2. Through instance method for e.g get data()
  3. By using constructor method to initialize an object when it is first created.
  • Constructors have the same name as class itself.
  • Constructors do not specify a return type.
  • Constructors is called automatically by the run time system.
For e.g.

class Rectangle
{
int length,width;
Rectangle(int x, int y) //constructors
{
length = x;
width = y;
}
int rectArea()
{
return(length*width);
}
}

class ABC
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Rectang lerect1=new Rectangle(15,10) //calling constructor
int area1=rect1.rectArea();
System.out.println(“Area1 = “+ area1);
}
}


                                                              Constructors

If you do not provide any constructors, a default no-arg constructor is provided for you. This constructor takes no arguments and does nothing. If you want a specific no argument constructor as well as constructors that take arguments, you must explicitly provide your own no argument constructor.

DEFAULT CONSTRUCTOR:
If no constructors are declared in a class, the compiler will create a default constructor that takes no parameter.
When you create a new Movie object, the compiler decides which constructor to call, based on the argument specified in parentheses in the new statement. Constructor can call another constructor of the same class by using this() syntax. By using this(), you can avoid duplicate code in multiple constructors. This technique is used when initialization routine is complex.

Public class Movie
{
private String title;
private String rating;
public Movie()
{
this(“G”);
}
public Movie (String newRating)
{
raing = newRating;
}
}
RULES: The call to this() must be the first statement in the constructor. The argument to this() must match those of the target constructor.