Results 1 to 2 of 2

Thread: Overloading Constructors in Core Java

  1. #1
    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:

    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.

  2. #2
    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.

Similar Threads

  1. Constructors in Java help
    By cloud101 in forum Software Development
    Replies: 4
    Last Post: 18-01-2012, 05:02 PM
  2. how to use multiple inheritance in core java
    By Mewad in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:31 AM
  3. The way to operator overloading in Java
    By Kohlmann in forum Software Development
    Replies: 8
    Last Post: 18-09-2010, 09:57 PM
  4. Comparison between core java and advanced java
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 10:57 AM
  5. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,682,533.60516 seconds with 16 queries