Constructor woes

  • Thread starter Thread starter Shikari Shambu
  • Start date Start date
S

Shikari Shambu

Hi,
I have a class that is derived from another class.

Say, Class Child dervies from Class Parent

Class Parent has a constructor that takes two params

public Parent(string A, string B)

I want Class Child to have a constructor that takes just one param but calls
the parents two param constructor

Something like

public Child (string A)
{
B = "Hello";
// Call the two param constructor here
}

I had to do a new to acheive this

public Child(string A)
{
B = "Hello";
// Call the two param constructor here
new Child(A, B)
}

Is there any other ways of doing what I did? And, what is the impact of the
new within the constructor?

TIA
 
Hi Shambu,

What Mattias has mentioned is the right way of invoking a super class constructor. In your case, you are simply creating an instance of super class object(though the instance cannot be accessed becasue an identifier is not provided for the same).

When you invoke a super class constructor -as mentioned by Mattias- a super class instance is not created, but the features inherited by the subclass will get initialized
 
Hi Shambu,

What Mattias is mentioned is the right way of invoking a base class constructor.
In your , you are simply creating an instance of base class(though this object cannot be accessed as an identifier is not provided).

When we invoke a base class constructor -in the way Mattias has mentioned, a base class object is not created, but the featurs inherited by the sub class will get initialized.
 
oops!
I received an ERR on first submit, so made a second submit. Now both have been added..!
 
Back
Top