About virtual and abstract : Why does the repair-method display "Repair a sportbike " ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

About virtual and abstract : Why does the repair-method display "Repair a sportbike " ?

I am busy exploring the key-words virtual and abstract. I have a non-abstract class SportBicycle with 2 methods : Ride and Repair The methods should be inherited form the class Bicycle. For the method Ride, I use the new key-word to override the method in the base class. This works fine. For the method Repair, I was expecting an Error. I do override the method and I am not using virtual, override or new. But there is no error and the displayed message is "Repair a sportbike " Why can I override a method from the base class without using a keyword ? https://code.sololearn.com/c6m61mn9cU3V

1st Jan 2018, 9:41 PM
sneeze
sneeze - avatar
1 Resposta
+ 2
This is because c# tries to prevent errors and therefore implicitly overrides the method due to conflict as the inheriting class members have higher preference in this case. To work around this, you have to cast the inheriting class to its base class. This is shown in the code below. https://code.sololearn.com/c72HBjJY42c6/?ref=app
6th Jan 2018, 8:15 AM
Ritam Mukherjee
Ritam Mukherjee - avatar