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.

No comments:

Post a Comment