tags:

views:

108

answers:

4

How to create the instance of child class in C#?

class Parent
{
 virtual void test()
 {
  Console.writeline("this is parent");
 }
}

class Child : Parent
{
 override void test()
 {
  Console.writeline("this is from child");
 }
}

public static void main()
{
 //Which one is right below?

 Child ch = new Child();

 Parent pa = new Parent();

 ch = new Parent();

 pa=new Child();

 ch.test();

 pa.test();
}
+2  A: 

If you want an instance of Child, then new Child() is the right thing to do. However, as Child is a specialization of Parent, you may refer to it either through a Child or a Parent reference (ch and pa in your example).

So you have to decide if you want to access the instance as a Child or a Parent.

If you do

Child ch = new Child();

You have a reference, ch of the type Child pointing to an instance of Child.

If you do

Parent pa = new Child();

You have a reference, pa of the type Parent pointing to an instance of Child. I.e. you're taking advantage of the fact that inheritance builds a "is a" relationship between Parent and Child.

In other words the type Child is a specialization of Parent. Thus an instance of Child can be used anywhere an instance of Parent is needed.

Brian Rasmussen
i want to go for the instance of child ... which like out of above 4 i need to call?
If you call `new Child()`, you will get an instance of the type `Child`, but as I stated you can use either a reference of type `Child` or `Parent`. What are you trying to accomplish besides creating the instance?
Brian Rasmussen
what do you mean by reference type of parent or child? does reference type mean the left side part of "=" sign?
The type of `ch` is `Child` in you example and the type of `pa` is `Parent`. Either of these may reference an instance of the type `Child`. However, `ch` may not reference an instance of `Parent` as `Parent` cannot be considered a type of `Child`.
Brian Rasmussen
+1  A: 
child ch = new child();
Amgad Fahmi
or Child ch = new parent();which is correct ?
no it's not correct it's different class it will not compile
Amgad Fahmi
+3  A: 

In your code you have four instantiations, that all mean slightly different things:

// create a new instance of child, treat it as an instance of type child
Child ch = new Child();

// create a new instance of parent, treat it as an instance of parent
Parent pa = new Parent();

// this will not compile; you cannot store a less specialized type in a variable
// declared as a more specialized type
Child ch = new Parent();

// create a new instance of child, but treat it as its base type, parent
Parent pa = new Child();

Which one (of those three that works) that is correct depends on what you want to achieve.

Note that the following two cases both print "this is from child":

Child ch = new Child();
ch.test();  

Parent pa = new Child();
pa.test();
Fredrik Mörk
+1  A: 

this tends to be more fundamental, than you thought! i recommend you reading a paper which explains you inheritance & polymorphism, eg msdn, msdn or codeproject

to me it's more about giving an explanation rather than a solution ...

Andreas Niedermair