C
C#3w ago
Faker

✅ When to use interfaces and why

Hello guys, sorry to disturb you all; I understood how to implement interfaces, thinking of it like a kind of contract that we can implement. The thing is, say I need to perform some CRUD operations on a particular entity, say Student entity, I would create a class for the student CRUD operations and create methods like adding new students, deleting existing student etc... My question is, in such cases, is it a good choice to just define an interface that list the method to be used for the CRUD operations? If so, can someone explain why please
25 Replies
Jimmacle
Jimmacle3w ago
with those requirements alone, no
phaseshift
phaseshift3w ago
inversion of control is a topic you can read up on
Jimmacle
Jimmacle3w ago
interfaces are mainly useful when you have multiple implementations that can be used interchangeably if you had different classes that stored students different ways for example, there would be a use case for an interface so your consuming code doesn't need to know exactly what type is handling the operations
Faker
FakerOP3w ago
the thing is, I would have several entities, like student, instructors
phaseshift
phaseshift3w ago
why is that ' a thing'? what is the underlying question?
Faker
FakerOP3w ago
hmm I'm thinking of an interface more like: I know instructors and students would have the same methods, so we need an interface to define the methods but I think it's not that at all
phaseshift
phaseshift3w ago
no, you have a crud system. the crud system should define the interface then if anything wants to do crud stuff for its types then it implements the interface for the types
Faker
FakerOP3w ago
Ah, here, in my use case, it would be inappropriate to use an interface though? Because the thing is, when we define an interface, that other implementations override the methods defined in their own way, here we would not override anything ? that's what you mean by "multiple implementations" ?
Jimmacle
Jimmacle3w ago
no, multiple implementations that can be used interchangeably
Faker
FakerOP3w ago
Like several classes implementing the same method but each one have their own way of working ?
Jimmacle
Jimmacle3w ago
as in, you could theoretically use any of them to accomplish the same high level task like if you had a concept of a "notification service" and had different implementations where one could send an email, another could send a text, etc. the high level goal is to send a notification, but the code sending it shouldn't have to know exactly what that means
Faker
FakerOP3w ago
I'm just reading a bit, will come back, just a small question... Say we have:
C#
interface IInterface1
{
// some code
}

public class A : IInterface1
{
// some code
}
C#
interface IInterface1
{
// some code
}

public class A : IInterface1
{
// some code
}
My question is, here if we create an instance of A, its data type is either A or IInterface1? What about if we create an instance of IInterface1? If we have: IInterface1 interface = new IInterface1 Is interface of type Ahere also? Does an interface have the same data type of whatever class implementing it ?
Jimmacle
Jimmacle3w ago
that question doesn't make sense an instance of A is always an instance of A even if you cast it to an interface or base class, the actual type of the object doesn't change
Faker
FakerOP3w ago
hmm I was thinking like when we inherit from classes, say we have class Person and Student; if Student inherit from Person, then a student is also a person
Jimmacle
Jimmacle3w ago
in that sense yes, but the actual object type is still Student
Faker
FakerOP3w ago
yeah I see Consider this:
C#

public interface INotificationService
{
void SendNotification(string message);
}

public class EmailNotificationService : INotificationService
{
public void SendNotification(string message)
{
// Code to send an email
Console.WriteLine("Email sent: " + message);
}
}

public class SmsNotificationService : INotificationService
{
public void SendNotification(string message)
{
// Code to send a text message
Console.WriteLine("SMS sent: " + message);
}
}

class Program
{
static void Main()
{
// We can decide which implementation to use at runtime.
INotificationService notificationService = new EmailNotificationService();

// We don't care how the notification is sent; we just call the method.
notificationService.SendNotification("Hello, this is your notification!");

// Later, if you decide to switch to SMS notifications:
notificationService = new SmsNotificationService();
notificationService.SendNotification("Hello, this is your notification via SMS!");
}
}
C#

public interface INotificationService
{
void SendNotification(string message);
}

public class EmailNotificationService : INotificationService
{
public void SendNotification(string message)
{
// Code to send an email
Console.WriteLine("Email sent: " + message);
}
}

public class SmsNotificationService : INotificationService
{
public void SendNotification(string message)
{
// Code to send a text message
Console.WriteLine("SMS sent: " + message);
}
}

class Program
{
static void Main()
{
// We can decide which implementation to use at runtime.
INotificationService notificationService = new EmailNotificationService();

// We don't care how the notification is sent; we just call the method.
notificationService.SendNotification("Hello, this is your notification!");

// Later, if you decide to switch to SMS notifications:
notificationService = new SmsNotificationService();
notificationService.SendNotification("Hello, this is your notification via SMS!");
}
}
Here, I didn't understand why our interface, INotificationService can create a new instance of EmailNotificationService
Jimmacle
Jimmacle3w ago
the interface isn't creating an instance of anything you're creating an instance and assigning it to a variable of a less specific type the variable can store a reference to any instance of a type that implements INotificationService
Faker
FakerOP3w ago
ahhh I see it's clearer now, thanks !! Will come back with other questions By the way, is there a reason why the variable can store the reference of ANY thing implementing the INotificationService? I mean, I tend to think of it as a Parent-Child relationship in terms of classes (don't know if it's a good way of thinking for interfaces though, please correct me if not :c)... The thing is I imagined the interface as the child class. So basically, we are assigning an instance of the parent class to a child class, is that possible? Sorry, this is out of the context of the question, more about inheritance I think :c
Jimmacle
Jimmacle3w ago
it's not about parent/child, it's about "is a" relationships this could be a child class or a class implementing an interface if anything, an interface would be considered a parent class in your example
Faker
FakerOP3w ago
yeah exactly, why the parent here
Jimmacle
Jimmacle3w ago
why would it be the child?
Faker
FakerOP3w ago
Oh I think I thought of it as follows: class student : Person whatever is found to the right of the : would be the child but this it is the parent , not the child sorry, I got confused Now I understand why we can write: INotificationService notificationService = new EmailNotificationService(); sorry, one more question, why can the variable store the instance type of anything that implements INotificationService? This is because, if a class implements the INotificationService, then it's instance is also considered an instance of INotificationService? But the data type stays the same though ?
ACiDCA7
ACiDCA73w ago
close enough. there are some caviats but in general well summarized
Faker
FakerOP3w ago
Hi, I was reading about interface and all, the more I read about them, the more often I saw the words inversion of control and dependency injection. I was wondering if you can recommend a good book where I can learn them or any design principle associated to them Yeah I see, is it correct to say the following please: So basically, say I want to save students; I may have the option of either storing them in a database or in a file. The consuming code, what it can do is either assign an instance of a database operation to the interface that both my database and file class implements. (something like IStorage storage = new DbStorage();) Then what happens, we can do something like StudentService service = new StudentService(storage) We don't know storage refers to what, it can be a database or a file.
Meas
Meas3w ago
It's correct to say you, as a developer, don't need to know when working inside StudentService. But the runtime will still know or it wouldn't be able to call the right implementation for the interface at runtime.

Did you find this page helpful?