✅ 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 please25 Replies
with those requirements alone, no
inversion of control is a topic you can read up on
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
the thing is, I would have several entities, like student, instructors
why is that ' a thing'? what is the underlying question?
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
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
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" ?
no,
multiple implementations that can be used interchangeably
Like several classes implementing the same method but each one have their own way of working ?
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
I'm just reading a bit, will come back, just a small question...
Say we have:
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 A
here also?
Does an interface have the same data type of whatever class implementing it ?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
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 personin that sense yes, but the actual object type is still
Student
yeah I see
Consider this:
Here, I didn't understand why our interface,
INotificationService
can create a new instance of EmailNotificationService
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
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 :cit'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
yeah exactly, why the parent here
why would it be the child?
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 ?close enough. there are some caviats but in general well summarized
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.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.