Constructor woes

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
 
G

Guest

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
 
G

Guest

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.
 
G

Guest

oops!
I received an ERR on first submit, so made a second submit. Now both have been added..!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top