Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Constructor from another Constructor

Tags:

java

c#

oop

I want to do something like this

public class Class1
    {
       public Class1()
       {

       }
       public Class1(int a)
       {

       }
    }
   public class Class2 :Class1
    {
       public Class2(int a)
       {

       }
       public Class2(): base(2)
       {
         this(2);   // new Class2(2);
       }

    }

I know this can't be achieved in Java (can use one between (super or this) in the first line)

But somehow I am in need of this kind of work how to achieve that? Means calling the base class's parameterised and derived class's parameterised constructor in default constructor of derived class.

like image 716
Nits Avatar asked Aug 03 '10 04:08

Nits


2 Answers

MSDN article on constructors is pretty good. Here are some relevant bits:

A constructor can use the base keyword to call the constructor of a base class.
....
A constructor can invoke another constructor in the same object using the this keyword. Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression.

This should work:

public class Class1
{
   public Class1()
   {

   }
   public Class1(int a)
   {

   }
}
public class Class2 :Class1
{
   public Class2(int a) : base(a)
   {

   }
   public Class2(): this(2)
   {
   }

}
like image 184
Igor Zevaka Avatar answered Oct 17 '22 21:10

Igor Zevaka


Igor's answer is a fine example of how you should write the constructors in this situation. To address the more general case of your final sentence: you can't chain to more than one constructor. You can't call a base constructor and another constructor in the current class.

There are two typical patterns for overloaded constructor. In the first pattern, the set of overloads of the derived class roughly matches the set of overloads for the base class - you try to make the derived class feel like it's inherited the constructors, effectively. (Constructors themselves aren't inherited, but if you provide the same signatures then it feels like it to the caller.) This is typically the case when your derived class doesn't need additional information. Of course each constructor can have extra parameters, and only pass a subset up to the base constructor, but that can start to get complicated.

In the second pattern, you have several constructors in the derived class each of which calls a "master" constructor in the same (derived) class. This master constructor has the most parameters, as it needs to be able to handle everything specified by any of the other constructors. Sometimes the master constructor should be private, if some combinations wouldn't make sense, but are convenient to specify in one place when you know you can only reach the code via a sensible public constructor. In this case, only that "master" constructor chains directly to the base class constructor. This is typically used when the derived class has several additional pieces of information beyond what the base class needs.

There are hybrids of this pattern where you have multiple masters with "groups" of overloads calling the masters... but I'd advise you to try to keep it simple where possible. Also consider the possibility of providing static factory methods instead of constructors - that can end up making for more readable code as you can name the methods by their purpose/parameters - see TimeSpan.FromMinutes for example.

like image 27
Jon Skeet Avatar answered Oct 17 '22 19:10

Jon Skeet