❔ C# abstract classes & interfaces

So I normally code in java, and I was wondering, are there any major differences in terms of abstract classes and interfaces in C#, I thought C# had multi inheritance so then what’s even the point of interfaces.
47 Replies
JakenVeina
JakenVeina2y ago
C# does not have multiple-inheritance thus, interfaces C# does KINDA SORTA have multiple-inheritance, but not really, in the form of Default Interface methods, added in C#8
Itskillerluc
ItskillerlucOP2y ago
Oooh I thought it did oh well java has that too But abstract methods would be better for that, even tho I still don’t see what the real difference is if they have default methods I guess fields
JakenVeina
JakenVeina2y ago
interfaces are a decent bit more flexible and no, abstract methods don't allow for multiple inheritance interfaces (sorta) do there's not a WHOLE lot of practical difference between base/overridable methods and interface default implementation methods interface default implementations exist mainly because they didn't used to and there was a decent use-case for adding support
Thinker
Thinker2y ago
For all practical intents and purposes, C# does not have multiple inheritance.
JakenVeina
JakenVeina2y ago
(I believe) default interface implementations are just a trick of the compiler a class that implements an interface with a default implementation isn't actually INHERITING that implementation, the compiler is effectively copy/pasting it into the class like a mixin I think
Thinker
Thinker2y ago
The point of interfaces is to provide a contract that a type has to implement. Abstract classes provide base implementations and methods. They're completely separate purposes.
JakenVeina
JakenVeina2y ago
yeah what interfaces REALLY allow for is polymorphism "I, as a consumer, need something that does X. I don't care what or how." and yes, you can accomplish the same thing with base/abstract classes, but that's not quite what those are for base/abstract classes are mainly about de-duplicating logic and functionality interfaces have neither of those things (default implementations notwithstanding)
Itskillerluc
ItskillerlucOP2y ago
Ye but At that point you would never actually have to use abstract classes I guess the difference is that abstract classes can have fields which makes them very different But ye But fair enough then they are the exact same as in java
JakenVeina
JakenVeina2y ago
abstract classes contain logic and state, yes interfaces do not
Itskillerluc
ItskillerlucOP2y ago
Wdym logic and state? I mean I see of interfaces as being a static thing Like ye the methods aren’t static but like the interface itself is Ok but if there are no special things in c# for that then I know it all
Thinker
Thinker2y ago
They're not about state, they're about what you can do with a class You pretty much never have to use them unless you explicitly want to inherit behavior, for instance for base implementations of interfaces.
Itskillerluc
ItskillerlucOP2y ago
Well and one big thing is that abstract classes can inherit other classes, so you can actually limit the use. Like you can have EatingAnimal which extends Animal so only like Cat and Dog can override the eat method, unlike interfaces where you could make a house an EatingAnimal which is…. Cursed
Thinker
Thinker2y ago
Well, a house implementing that interface wouldn't make sense A house could also inherit from Animal if it wanted to
Itskillerluc
ItskillerlucOP2y ago
Well not if it already inherits building
Thinker
Thinker2y ago
You could likewise have an Animal and IEatingAnimal : IAnimal interface yeah
Itskillerluc
ItskillerlucOP2y ago
I guess that’s the point of only allowing inheritance of one class
Jimmacle
Jimmacle2y ago
until you want to include carnivorous plants in your model and now inheritance doesn't seem like such a good idea 😛
Thinker
Thinker2y ago
But then you run into the issue of a caravan being both a house and a vehicle. You'd ideally want to inherit from both House and Vehicle, but you can't. However, you could implement both IHouse and IVehicle just fine.
Itskillerluc
ItskillerlucOP2y ago
Eh ye but that’s def not something I would use an interface for Because a house and a vehicle aren’t something you add to a thing Its more of a thing on its own IMovable would be better then
Jimmacle
Jimmacle2y ago
it's a thing that works as two different things the granularity of your interfaces depends on your needs
Itskillerluc
ItskillerlucOP2y ago
Ye but like the class Vehicle def requires fields And you can’t do that with an interface So you’ll have a problem I guess you can do abstract getters and setters but I don’t like those Makes your code so big imo kind of stupid Like at that point you should use a abstract class Unless C# has a solution for this?
Jimmacle
Jimmacle2y ago
the class implementing the interface can implement it however it wants interfaces define contracts, they aren't instantiable types they tell a consumer how it can use the object, the implementation details are irrelevant
Itskillerluc
ItskillerlucOP2y ago
Ye so a House and Vehicle shouldn’t be an interface imo
Jimmacle
Jimmacle2y ago
then what should a caravan be? because like thinker said, it's a house and a vehicle
Omnissiah
Omnissiah2y ago
for jeremy, trashed
Jimmacle
Jimmacle2y ago
if you have a function that does something with an object that works like a house, how would you pass it a caravan?
Thinker
Thinker2y ago
Or vice versa with a vehicle
Omnissiah
Omnissiah2y ago
if it has to be something set at runtime then it can't be inheritance it must be composition i would say it depends this is going from meta-talk to details about implementation (it could even be a data approach, object is registered somewhere both as a house and as a vehicle)
Jimmacle
Jimmacle2y ago
everything depends in programming generally it depends if it depends
Omnissiah
Omnissiah2y ago
can i? $itdepends
Itskillerluc
ItskillerlucOP2y ago
Lol why an elephant
Omnissiah
Omnissiah2y ago
because they are wise
Itskillerluc
ItskillerlucOP2y ago
Lmao I guess
Thinker
Thinker2y ago
based on this (an actual series of books)
Anton
Anton2y ago
Abstract classes are good when you want to split up an implementation into substeps. You can define a sealed implementation of an interface method in an abstract class, that just calls into abstract or virtual methods and maybe adds some glue around those calls. That's basically all I typically use them for. Defining abstract classes for mixing in some helper code is an overused malpractice in my opinion
Florian Voß
Florian Voß2y ago
this is called template method design pattern and it can be done using interfaces too since c# 8.0 you use abstract class for it in old c# version because interfaces don't support default implementations. Since they do you better use interfaces for that I'd say we use abstract class over interfaces when we want to inherit instance variables like fields and properties
Anton
Anton2y ago
you can seal methods on interfaces?
Florian Voß
Florian Voß2y ago
no you do it without sealed then
Thinker
Thinker2y ago
Is this the name of a pattern? Because I've never heard this term.
Florian Voß
Florian Voß2y ago
public interface ITextValidator{
public bool AllCharsValid();
public bool ValidLength();
public bool IsValid(){
return AllCharsValid() && ValidLength();
}
}
public interface ITextValidator{
public bool AllCharsValid();
public bool ValidLength();
public bool IsValid(){
return AllCharsValid() && ValidLength();
}
}
here a very basic exmaple AllCharsValid and IsValidLength get implemented by concrete validator. IsValid() is our template method that declares the structure of our algorithm without concrete implementation
Thinker
Thinker2y ago
That could just be an extension method catsip
Florian Voß
Florian Voß2y ago
right ok other example, suppose we have a fight method that always contains certain steps like draw weapon, aim, shoot, whatever. But each weapon does those steps differently. We can have template method fight that calls these methods which get redefined it is the name of the pattern, yes where you give general structure / order of algorithm but leave concrete implementation of the steps to concretions, the method that declares the structure and doesnt get redefined is a "template method" its part of the original GoF patterns
Thinker
Thinker2y ago
GoF patterns?
Florian Voß
Florian Voß2y ago
Gang of Four
Thinker
Thinker2y ago
Also btw I don't think this has to do with the original question anymore
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server