Ofelia Muradova
Ofelia Muradova

Reputation: 157

How to make a subclass implement an interface?

I have created a class and a subclass. I also have an interface in the parent class. How can I make the child class implement the interface?

The code is:

class Car
{ 
    public interface ISmth
    {
        void MyMethod();
    }
}
class MyCar : Car
{ 
    //implement the interface 
}

Upvotes: 2

Views: 4064

Answers (2)

Enigmativity
Enigmativity

Reputation: 117064

This is how your code should probably be laid out.

public interface ISmth
{
    void MyMethod();
}

class Car
{
}

class MyCar : Car, ISmth
{
    public void MyMethod()
    {
    }
}

There doesn't seem to be a reason to nest ISmth in Car, but if you do have one you certainly could do it.

Upvotes: 4

Anton Z
Anton Z

Reputation: 133

You need to declare MyCar like that:

class MyCar: Car, Car.ISmth {}

Upvotes: 3

Related Questions