Does creating an instance of a child class create an instance of the parent class?

  • A+
Category:Languages

I'm new to C#, and I wanted to know, that if I create an instance of a child class, does it also automatically create an instance of the parent class or what?

Here is my code:

class Program {      public class ParentClass     {         public ParentClass()         {             Console.WriteLine("ChildClass uses my Ctor ");         }      }      public class ChildClass : ParentClass     {         public ChildClass()         {             Console.WriteLine("SaySomething");         }     }      public static void Main()     {         ChildClass child = new ChildClass();     } } 

 


does it also automatically create an instance of the Parent class?

Not a separate instance; the ChildClass is a ParentClass instance, when talking about inheritance.

In words, this is like:

when creating a dog, do we also create an instance of an animal?

We don't create a dog and (separately) create an animal; the dog is the animal instance. And if we create a poodle, the poodle is the dog and is the animal.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: