C
C#2y ago
Elio

Interface

hi, is it a good practice to make an interface and use it like this IPoint p = new Point(2, 3); or should i write an abstract class ?
13 Replies
Angius
Angius2y ago
In this particular example, it doesn't really make sense to have an interface, IMHO But, in general, use an abstract class if you need some baseline functionality that's the same for every type that inherits it And use an interface when you just need the contract
Elio
Elio2y ago
the contract mean the method which are supposed to be inherited by the class ?
Angius
Angius2y ago
Not inherited, implemented But yes
Elio
Elio2y ago
ok then if i want to pass method and variable i should choose to use abstract ? and override the method
Angius
Angius2y ago
Pass a method and variable... where? To another method? Or do you mean you want Point to implement/inherit a method and a property? Right, one more difference: interfaces cannot have fields So if you need a field, you want an abstract class
Elio
Elio2y ago
ok because on the example above there is field no ? on the interfact
Angius
Angius2y ago
This example? There are no fields there, just properties Properties are public and have { get; set; } (or { get; } or { get; init; } or similar) Fields are private and don't have the curly brace things
Elio
Elio2y ago
ok i should go search the difference to understand if i need abstract or interface then between propety and field cause to me it feel like it's the same thing
Elio
Elio2y ago
like this does it make sens to use the interface ? (both class communication implement Icommunication)
Elio
Elio2y ago
in order to "selec" the good class depends on a parameter
Angius
Angius2y ago
Yeah
Elio
Elio2y ago
ok then i will go on something like that thanks for your help