Instantiate variables in Base Classes, usable in Derived Classes
Hi, I'm playing a little with inheritance but I've a big problem that I'm not able to solve. I created a base class Hero and a derived class Epic. The idea is to have a class that describes general heroes [Superheroes, Epic Heroes, War Heroes, etc], and the derived classes more specific. There is one big problem though and maybe you can help me. Each hero has a name [also the general heroes of tha base class]. If I define the name via constructor in the derived class no problems arises, but if I try to do it in the base one nothing works. -I thought I could instantiate the name variable in the base class only [as derived classes inherit from it] but it seems not working. -I also overridden the constreuctor of the base class but still nothing worked. Do you know how can I give my hero object a name in the base class [so i can have general heroes with a name] and using the name in the derived classes [if my object is created in such a way]? I post the code that so far works for a better understanding. Thank you for your help! using System; class MainClass { class Hero{ public Hero(){ Console.WriteLine("Hero created"); } public virtual void shout(){ Console.WriteLine("The hero shouts!"); } } class Epic:Hero{ public string name {get; set;} public Epic(string nm){ name = nm; Console.WriteLine("Epic hero created, " + name); } public override void shout(){ Console.WriteLine("The epic hero shouts!"); } } public static void Main (string[] args) { Epic achil = new Epic("Achil"); achil.shout(); } }