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
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
the contract mean the method which are supposed to be inherited by the class ?
Not inherited, implemented
But yes
ok then if i want to pass method and variable i should choose to use abstract ?
and override the method
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 classok because on the example above there is field no ?
on the interfact
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 thingsok 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
like this does it make sens to use the interface ? (both class communication implement Icommunication)
in order to "selec" the good class depends on a parameter
Yeah
ok then i will go on something like that thanks for your help