+ 1

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(); } }

1st May 2017, 12:01 PM
Daniele
Daniele - avatar
4 Answers
+ 3
Ahh, a pretty simple issue. You never tell the base class how to set a name, what their name is, and you never define the name variable in it. You've mentioned that you tried to override the constructor, but you see, what you instead need to do is call the super.constructorName . Instead of re-writing it, simply write it into the base class and call it.
1st May 2017, 12:05 PM
Keto Z
Keto Z - avatar
+ 2
But let's say I want the Base class virtual method to shout "I'm the hero Achil" where Achil il the name... and I want the derived class to shout "I'm the Epic Hero Achil"? If I don't instantiate the name variable in the derived class it seems not working... how can I make it work both for Base class created objects and derived class created objects?
1st May 2017, 12:12 PM
Daniele
Daniele - avatar
+ 1
I tried to invert it, instantiating the name variable in the base class but it didn't work... using System; class MainClass{ class Hero{ public string name {get; set;} public Hero(string nm){ name = nm; Console.WriteLine("Hero created"); } public virtual void shout(){ Console.WriteLine("The hero shouts!"); } } class Epic:Hero{ public Epic(){ 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(); } }
1st May 2017, 1:51 PM
Daniele
Daniele - avatar
+ 1
No... I'm trying to have the name variable available both for the Base and the derived class... the idea is that the class hero is generic, while the class epic is a more specific version of hero... there are several kind of heroes like superheroes, war heroes, epic heroes, etc... while different types of heroes can have different features (superheroes have superpowers, epic heroes have divine parents, war heroes have special skills like good understanding of tactics...) all the heroes generically have a name... I'd like to create the Base class generic, and if I don't have a derived class I put the hero in there... so name should exist both for Base and Derived classes
1st May 2017, 7:22 PM
Daniele
Daniele - avatar