Science Fair Projects Ideas - Constructor overloading

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Constructor overloading

In object-oriented programming it is common to use constructor overloading to allow multiple ways of creating an object.

Remember, method overloading is when methods have the same name, but take different arguments. Method overriding is when a subclass provides a method with the same name and arguments as in a superclass.

Imagine you have a class Person that holds data about each person's name and phone number. In the (Java) example below the constructor is overloaded as there are two methods with the same name ("Person").

 public class Person {
     // instance variables:
     String name;
     String phoneNumber;

     // Constructor with name and number:
     public Person(String name, String phoneNumber){
         this.name=name;
         this.phoneNumber=phoneNumber;
     }

     // Alternative constructor without giving phone number.
     // This will set the phone number to "N/A" (for "not available")
     public Person(String name){
         this(name, "N/A");
     }
 }

In Java, the call this.name=name means "set this object's variable called name to the value of the argument name.


Note that the second constructor invokes the first one with the call this(name, "N/A"). This is a highly recommended programming practise. It might seem more natural, and clearer, to write the second constructor above as:

public Person(String name){
    this.name=name;
    this.phoneNumber="N/A";
}


Which, in this case, will work fine and have just the same effect.

So, why should you bother calling the first constructor from the second?

Because it gives you more flexible code. Suppose, for some odd reason, that you want to store the names in lower case only. Then you only need to change one line: the line

this.name=name;

in the first constructor is changed to

this.name=name.toLowerCase();

If you have the same line duplicated in the second constructor, you will have to change that as well.

See also

factory method pattern

Last updated: 05-09-2005 17:08:26
10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice