Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-12-2008
Member
 
Join Date: Aug 2008
Posts: 88
Overloading Constructors in Core Java

Constructors can be overloaded just as regular methods. Here is an example of a class with an overloaded constructor:

Code:
public class date

        {

               int year;

               int month;

               int day;

               date(int year)

               {

                       this.year = year;

                       month = 1;

                       day = 1;

               }

               date(int year/int month,int day)

               {

                       this.year = year;

                       this.month = month;

                       this.day = day;

               }

        }
You could create an object of that class with statements like either of the following:

Quote:
date a = new date(2010];
date b = new date(2010,5,15);
Naturally, the first statement will call the first constructor, because it has just one parameter. The second statement has three parameters, so it will call the second constructor.

If a class has at least one constructor, then a constructor will always be called when an object is created. This means you must always supply the appropriate parameters for at least one of the constructors.
Reply With Quote
  #2  
Old 23-12-2008
Member
 
Join Date: Aug 2008
Posts: 88
Re: Overloading Constructors in Core Java

The same rule applies to constructors of a superclass. If a class has a superclass and that superclass has at least one constructor, then a constructor of the superclass must be called when an object is created. It is generally the responsibility of the subclass constructor to make a call to a superclass constructor. If the subclass constructor doesn't call the superclass constructor and the superclass has a constructor with no parameters, Java will call that superclass constructor automatically. But, if all the superclass constructors require parameters, java won't be able to call one of them--in that case, the subclass constructor is required to make a call.

Here is an example:

Code:
class animal

        {

               animal()

               {

                       /* Java code here */

               }

        }

 

        class cat extends animal

        {

               cat()

               {

                       super();

                       /* Java code here */

               }

        }
In this example, the subclass constructor is calling the superclass constructor. (Notice how it uses the keyword super to do that.) In this case, the call to super isn't required. Since the superclass constructor has no parameters, Java will call the superclass constructor if the subclass constructor doesn't.

Here is another example:

Code:
class animal

        {

               animal(int age)

               {

                       /* Java code here */

               }

               

        class cat extends animal

        {

               cat()

               {

                       super(5);

                       /* Java code here */

               }

        }
In this example, there are no superclass constructors without parameters. That means the subclass constructor is required to call the superclass constructor.

When a subclass constructor calls a superclass constnxctor, it must be done at the very beginning of the subclass constructor No other processing can be done first.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Overloading Constructors in Core Java"
Thread Thread Starter Forum Replies Last Post
Constructors in Java help cloud101 Software Development 4 18-01-2012 05:02 PM
how to use multiple inheritance in core java Mewad Software Development 4 08-01-2011 10:31 AM
The way to operator overloading in Java Kohlmann Software Development 8 18-09-2010 09:57 PM
Comparison between core java and advanced java Prashobh Mallu Software Development 5 19-01-2010 10:57 AM
What is method overriding and method overloading in java beelow Software Development 3 17-11-2009 08:20 AM


All times are GMT +5.5. The time now is 02:51 PM.