tags:

views:

129

answers:

3

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.

+2  A: 

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)
   {
   }

}
Igor Zevaka
The only possible problem I can see here would be if you **didn't** want `Class2(int a) : base(a)`, but **did** want `Class2() : base(2)`. However, I can't think of a class structure where such a decision would be the best design.
Brian S
that I know, but I want to execute two of them in default constructor of derived class that is I want to execute parameterised constructor of base as well as of derived through the default constructor of derived class(can write somewhere in defination)
Nits
Right, you cannot do that, you can only call *one other constructor*, not both. I think you should rethink the design that requires constructors to be called in this way.
Igor Zevaka
that's what I wanted to know if we can do that somehow, also if you can better make me understand then why both in JAVA and C# this type of thing is not given. Like we can use likepublic Class2(int a) : base(a) but can't use in defination of the default constructor of derived class
Nits
+1  A: 

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.

Jon Skeet
+1  A: 

Thats impossible in both languages (for a good reason). Otherwise you would call the base-constructors multiple times due to the implicit base call if theres no explicit base or this. This could lead to unwanted behaviour.

atamanroman
this also looks good to me. But what is the harm if we call the more time the same base construtor
Nits
A constructor is supposed to run only once. If its only easy initialization like setting default variables theres no problem (except for performance since you do the task twice) but if you would populate a list in your constructor, you had all the entries twice. I think there are better reasons, for me it just feels unsafe. Maybe someone can provide a better example?
atamanroman