C
C#3d ago
Faker

✅ Interface in C#

Hello, I have a question, consider the following code:
C#
// Define an interface
public interface IAnimal
{
void Speak();
}

// Implement the interface in a class
public class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Woof!");
}
}
C#
// Define an interface
public interface IAnimal
{
void Speak();
}

// Implement the interface in a class
public class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Woof!");
}
}
C#
IAnimal myDog = new Dog();
myDog.Speak(); // Output: Woof!
C#
IAnimal myDog = new Dog();
myDog.Speak(); // Output: Woof!
My question is, an interface is just like an abstract class in the sense that we can' instantiate directly but rather should be instantiated indirectly by using a derived class from it. When we write:
C#
IAnimal myDog = new Dog();
myDog.Speak();
C#
IAnimal myDog = new Dog();
myDog.Speak();
I don't understand, we are creating a Dog object, this references a dog? What is the IAnimal data type? What does it represents and why not use Dog instead?
22 Replies
mtreit
mtreit3d ago
An interface defines a contract that the class implements. You use the the interface to allow implementing generic code in the sense that it doesn't care what the underlying type is.
public void MakeItSpeak(IAnimal animal)
{
// code goes here
}
public void MakeItSpeak(IAnimal animal)
{
// code goes here
}
This code can work with anything that is an IAnimal. That underlying type in your code example is Dog. Where it matters is when you hand it off to other code, as in my example.
Faker
FakerOP3d ago
it's mandatory to overwrite methods from an interface even if we don't use them?
Keswiik
Keswiik3d ago
if they don't provide a default implementation, yes
Pobiega
Pobiega3d ago
you must implement all methods on an interface
Faker
FakerOP3d ago
Hmm here under the hood, we are referencing a dog in memory, but what does the IAnimal mean? yeah I see, like if they don't have a body, then we should implement them ?
mtreit
mtreit3d ago
Yes, although you can throw NotImplementedException for methods you do not support (although best to support everything if possible.) It just means the variable is of type IAnimal. You could later assign a different type of animal to that variable. If the variable was of type Dog you couldn't assign a Cat to it.
Faker
FakerOP3d ago
oh ok
mtreit
mtreit3d ago
A variable of type IAnimal can accept either.
Faker
FakerOP3d ago
on a generic term, it's an animal but under the hood, more precisely it's a dog ?
mtreit
mtreit3d ago
Exactly.
Keswiik
Keswiik3d ago
might be beneficial to read up on polymorphism and inheritance
Faker
FakerOP3d ago
yep will do so, I'm currently refreshing the OOP concepts
Jdizzle
Jdizzle3d ago
:base(…) is always referring to the interface if you see that too
mtreit
mtreit3d ago
No
Keswiik
Keswiik3d ago
no, it isn't, please don't add in misinformation. interfaces do not have constructors
Jdizzle
Jdizzle3d ago
It was a question forgot ???
mtreit
mtreit3d ago
Interface is not the same as a base class.
Jdizzle
Jdizzle3d ago
That’s for abstract right ?
Keswiik
Keswiik3d ago
doesn't have to be abstract
Jdizzle
Jdizzle3d ago
Private constructor ?
Keswiik
Keswiik3d ago
I would assume not because the inheriting class shouldn't have visibility, but these questions are pretty off-topic for this thread. Might want to open up your own question if that's something you need answers for
Jdizzle
Jdizzle3d ago
Oh ok .

Did you find this page helpful?