5

I have a theorical/pratical question about how inheritance works in C#.

Let's say that I have to model some autos, so I have a few common methods that all the autos must implement and a pilot should know. In code-words this should be an Interface:

interface Automobile { void speedup(); void break(); void steer(); //bla bla } 

Now, all kind of car should have some common technical implementations beside the actions that a pilot can perform over them, let's say the property fuelLevel, at this point I still don't want to know how a car speeds up or breaks down, so I would wrote:

 abstract class Car : Automobile { int fuelLevel; // for example.... public abstract void speedup(); public abstract void break(); public abstract void steer(); } 

Now I want to model a Ferrari, so:

class Ferrari : Car { public void speedup() { ... } public void break() { ... } public void steer() { ... } } 

Now, in Java this code should work (changing some syntax), but in C# won't because it says:

Error 1 'Ferrari' does not implement inherited abstract member 'Car.speedup()' Error 1 'Ferrari' does not implement inherited abstract member 'Car.break()' Error 1 'Ferrari' does not implement inherited abstract member 'Car.steer()' 

Why? How should I fix this problem?

3 Answers 3

16

You need to mark the methods with override to cover the abstract definitions beneath:

class Ferrari : Car { public override void speedup() { } public override void break() { } public override void steer() { } } 

Other than that, you're set - the Ferrari class will satisfy your interface, even though the methods are derived. Also as an aside, naming in C# is slightly different:

  • Prefix interfaces with I, IAutomobile.
  • Capital letters on methods: Speedup(), Break(), Steer().
Sign up to request clarification or add additional context in comments.

4 Comments

I guess this is one of the differences between Java and C#... Thank you so much.
@IssamTP actually according to the times on the site, it only took me 3 minutes to answer :-)
Yep but I tried to accept the ans. as soon as I've seen it, but I couldn't because the system said: "You can't accept an answer bla bla 7 minutes"...
@ IssamTP I thought you forget to accept the answer... cant stop myself to comment for that...
3

You are actually ignoring to override abstract class members. Try to consider something like:

interface IAutomobile { void Steer(); void Break(); } abstract class Car : IAutomobile { public abstract void Steer(); public abstract void Break(); } class Ferrari : Car { public override void Steer() { throw new NotImplementedException(); } public override void Break() { throw new NotImplementedException(); } } 

1 Comment

@IssamTP that's an automatic default implementation of the methods, so that even without you providing an implementation it will compile, no matter if the method should return a value or not.
1

well, "disconnetti" (are you italian?) method is not written anywhere, so you must before explain us where that comes from.

Second, In C# when you override an abstract method, you must explicit it through the override keyword

2 Comments

Yep I'm italian, the real code is slightly different from the posted because this example was easier to understand and to explain. Anyway I edited the code quite soon...
Ok, the answer is just that you need override keyword by the way, as I've written (and others too). Buona fortuna col codice ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.