Polymorphism | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Polymorphism

problem: The program you are given declares Unit class which has a method Attack(). It outputs "Using sword!". Derive Musketeer and Magician classes from the Unit class and override its Attack() method to output the corresponding messages while attacking: Musketeer => "Using musket!" Magician =>"Using magic!" Question: What I'm i doing wrong? My solution: namespace polymoprhism { class Program { static void Main(string[] args) { Unit unit1 = new Unit(); Unit musketeer = new Musketeer(); Unit magician = new Magician(); unit1.Attack(); musketeer.Attack(); magician.Attack(); Console.ReadKey(); } } class Unit { public virtual void Attack() { Console.WriteLine("Using sword!"); } } /*derive the class from Unit class and override Attack() method*/ class Musketeer : Unit { public override void Attack() { Console.WriteLine("Musketeer => Using musket!"); } } /*derive the class from Unit class and override Attack() method*/ class Magician : Unit { public override void Attack() { Console.WriteLine("Magician => Using magic!"); } } }

28th Jun 2021, 11:16 AM
Munene Rwigi
Munene Rwigi - avatar
3 Answers
+ 1
There before array => is refer to class name. So don't add it in output. 3 output statements you need only like : Using sword! Using musket! Using magic! Don't add " class _name =>" in override methods. hope it helps...
28th Jun 2021, 11:37 AM
Jayakrishna 🇮🇳
0
Yea i did that earlier and i still got it wrong
28th Jun 2021, 11:39 PM
Munene Rwigi
Munene Rwigi - avatar
0
If still not solved it, then Save your update code in playground and share link here.. add which lesson task it is. So I can try to find mistake.. (if it is not pro..).
29th Jun 2021, 12:30 PM
Jayakrishna 🇮🇳