Interfaces vs Abstract Classes

  Рет қаралды 13,803

Raw Coding

Raw Coding

Күн бұрын

A popular C# interview question is to explain the differences between interfaces and abstract classes, why use an interface over an abstract class or an abstract class over the interface.
Support my Work 🤝 / raw_coding
Buy my Courses 📚 learning.raw-coding.dev
Buy my Merch 🛒 shop.raw-coding.dev
Join the Community 💬 / discord
Tweet Tweet 📣 / anton_t0shik
Tune in LIVE! 🎥 / raw_coding
#csharp #dotnet #interview

Пікірлер: 49
@neelamrani6884
@neelamrani6884 8 ай бұрын
We need more of these interview tips gems
@81NARY
@81NARY 8 ай бұрын
Best lighting on the tubes, welcome back!
@lw5482
@lw5482 8 ай бұрын
one thing is, if you wanna change the interface like adding new method / property to it, it will break all implementations. And if you make new interface to replace it, you need to document it and educate other devs in the team. It's disaster especially for package / framework devs. So one important rule would be: If you are going to design an interface, design it very carefully and target it to the future.
@RawCoding
@RawCoding 8 ай бұрын
100% agree, interfaces on their own don't bring any degree of correctness.
@jonasgranlund4427
@jonasgranlund4427 8 ай бұрын
How about using interface Segregation if there are many implementations of the original interface where some of them dont need this new method, I like really small interfaces most of the time and in worst case there are a possibility to use a default implementation of the new method since c# 8.0, if you dont want all implementations of the "god interface" to explode when you add the 78th method. :)
@lw5482
@lw5482 8 ай бұрын
@@jonasgranlund4427 Actually the interfaces should always be "minimal" (as small as possible) to represent a specific contract (as you mentioned the solid principle). If an interface got like more than 20 methods (or even 10, it depends), it could much be a design failure. What I meant is, you need to think carefully about your "contract", like what belong to the interface and what not, method signature and possible demands in the future. Even an interface is minimal, it could still face the problem I mentioned.
@jonasgranlund4427
@jonasgranlund4427 8 ай бұрын
@@lw5482 I agree on that and I think that on your first point that if there are many places that implement this interface that need the new method you can add it to the "main interface and if the implementations cant implement it at once there is the possibility to do a default implementation of the new method to not break. But if one or more dont need the new method it makes sense with breaking it ut to its own interface or group it somewhere else where it makes sense. I use both but it is usually atleast 20 to 1 where I use interface compared to abstract class. Happy coding.
@andrii.h258
@andrii.h258 8 ай бұрын
In C#8 was introduced default methods for interfaces, so when you are adding new method in interface you can write some default logic to this method and do not break any implementation.
@Noceo
@Noceo 8 ай бұрын
Given that this would be a great topic for newer developers to understand better, I think the video would benefit from slowing down a little bit (or reducing the scope, if you want to keep it sub 15 minutes). Don't get me wrong, you are saying a lot of really important thing, but even with 5 years of .NET experience, I had a hard time keeping up on everything.
@RawCoding
@RawCoding 8 ай бұрын
noted, thank you
@trustingod0
@trustingod0 8 ай бұрын
I agree he really knows his stuff. Relaying that to others might be more difficult.
@Michael-lo3ht
@Michael-lo3ht 5 ай бұрын
Way too fast. I watched another video that was talking about the 2, but not why or a good explanation. So I found this one, hoping it would help. Talking fast and jumping around the screen. It's hard to keep up with all of it.
@trustingod0
@trustingod0 8 ай бұрын
Thanks for this video, very informative. Just a couple of questions about Abstract Classes compared to Interfaces. You mentioned that an abstract class is when you have a set of things that have a commonality and then you take that commonality and abstract it into an abstract class. Whereas with interfaces you said you want to separate the thing that a component is dependent on or the things that its consuming and this is where the bridge or contract comes in ultimately used to decouple your components into code. That went straight over my head lol. What I think I have discovered and why some people struggle with getting a sound understanding of the concepts is that maybe its because there is a coding benefit in using abstract classes and interfaces as well as practical advantage.
@RawCoding
@RawCoding 8 ай бұрын
what's the question?
@trustingod0
@trustingod0 8 ай бұрын
@@RawCoding Just a couple of questions about Abstract Classes compared to Interfaces. You mentioned that an abstract class is when you have a set of things that have a commonality and then you take that commonality and abstract it into an abstract class. Whereas with interfaces you said you want to separate the thing that a component is dependent on or the things that its consuming and this is where the bridge or contract comes in ultimately used to decouple your components into code. Just wodering if you could elaborate on that. I thought it was insightful that i mentioned that a lot of the coding paradigm has more of a readability or design benefit than necessarily any practical benefits, and I think thats where a lot of people get confused. For example to some people they think that when it says an interface is a contract that means just make sure that your classes implement the interface. But as you are designing a system it takes on a much different meaning. So there are levels to understanding and if you have an average understanding of interfaces you really dont understand them at all.
@victorchisomnwisu9776
@victorchisomnwisu9776 8 ай бұрын
He's back 🎉🎉
@noctavel
@noctavel 8 ай бұрын
good video, tho i would suggest going/running the code to exarcebate the difference between any 2 approaches. You did show the code, but it's hard to grasp between the 2 without running them
@amnesia3490
@amnesia3490 8 ай бұрын
oh my boi is back
@ManderO9
@ManderO9 8 ай бұрын
the legend is back
@shashikantnipanikar860
@shashikantnipanikar860 2 ай бұрын
That's a really great explanation. I think the interviewer wanted this from me. 😅
@m_stf
@m_stf 8 ай бұрын
welcome back
@neelamrani6884
@neelamrani6884 8 ай бұрын
Nice lighting😊😅
@oladipotimothy6007
@oladipotimothy6007 8 ай бұрын
I noticed I could use public, internal, and protected access modifiers in the interface. I couldn't use private though Update: doesn't work when inherited from another project. Insisted on public modifiers but worked when implemented 'explicitly' for protected modifiers regardless
@MultiBadway
@MultiBadway 8 ай бұрын
I like your wall its intresting
@fabianosobreira7581
@fabianosobreira7581 8 ай бұрын
😎
@ntson1710
@ntson1710 8 ай бұрын
what's your ide and what theme were you using in this video?
@EddieSundvall
@EddieSundvall 8 ай бұрын
It's Jetbrains Rider
@PrasannaCE
@PrasannaCE 7 ай бұрын
Hi, pls create a video to explain for Covariant and contravariant
@purplepixeleater
@purplepixeleater 8 ай бұрын
Abstract Base > Inherit > Implement abstracted target interface... avoids issues with dual inheritance. why not just use virtuals?
@andrewiecisa2907
@andrewiecisa2907 8 ай бұрын
Idea for content: openiddict-core
@yourockst0ne
@yourockst0ne 8 ай бұрын
🎉 nice seeing your FACE in my notifications
@RawCoding
@RawCoding 8 ай бұрын
Thank you Florian )
@robsonfaxas6574
@robsonfaxas6574 8 ай бұрын
Nothing related to coding, but I like the fact you didn't unwrinkle your t-shirt and don't care about the late afternoon sun removing half of your facial expressions at the beginning of your video.
@EdsonViniciusSchmitt
@EdsonViniciusSchmitt 8 ай бұрын
Hey there! You are missing a "t" on your thumbnail. Welcome back btw!
@RawCoding
@RawCoding 8 ай бұрын
Oooops! thank you
@damilareoyebanji2834
@damilareoyebanji2834 5 ай бұрын
"The Square at that point has schizophrenia "🤣🤣
@cocoscacao6102
@cocoscacao6102 8 ай бұрын
What's the editor?
@federicodibernardo2719
@federicodibernardo2719 4 ай бұрын
Rider (Jetbrains)
@allinvanguard
@allinvanguard 8 ай бұрын
Great video! I have two more points which were not mentioned (I think? Or maybe I also forgot haha) - A class can implement multiple interfaces, but only one abstract class. Also maybe the fact that default interface implementation methods can't act on any dependencies coming from the implementing class, it's essentially a static context.
@RawCoding
@RawCoding 8 ай бұрын
point 1 - was mentioned point 2 - that's wrong, in a static-context members belong to the type (there are no objects, only compile time), default implementation cant be called like: IInterface.DefaultMethod, they have to be called on an object the class for which implements the interface.
@allinvanguard
@allinvanguard 8 ай бұрын
@@RawCoding Oh, I see, then I missed that, my bad - For the second point I think I picked a poor wording, I meant that the context is basically the interface and potentially accessing other static contexts, but you can't interact with other dependencies like an abstract class / method can. Like, you can't just supply other dependencies to the interface itself unless you do it through property accessors. Just a minor point I figured might be interesting as well. On a side note, I think that a default implementation can be static as well?
@DucaTech
@DucaTech 8 ай бұрын
I think in C++, you can have multiple inheritance just not Java and C# using abstract classes.
@Bourn77
@Bourn77 8 ай бұрын
Mate, Dark theme please.
@rahiyansafin449
@rahiyansafin449 8 ай бұрын
first one to watch!
@ImReddyBasha
@ImReddyBasha 8 ай бұрын
Sir, please explain bit slow. Content not able to reaching.
@erhanalankus
@erhanalankus 8 ай бұрын
Thanks for the explanation. I think your voice tone and speaking rhythm could use a bit variety. I lost my attention while watching several times.
@ahlembs790
@ahlembs790 4 ай бұрын
slow down man ..u r talking too fast
When to use Traits, Interfaces, and Abstract Classes in PHP
15:08
Andrew Schmelyun
Рет қаралды 17 М.
ТАМАЕВ УНИЧТОЖИЛ CLS ВЕНГАЛБИ! Конфликт с Ахмедом?!
25:37
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 64 МЛН
Русалка
01:00
История одного вокалиста
Рет қаралды 6 МЛН
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 113 М.
C# interfaces vs. abstract classes - An EPIC combination?
5:28
tutorialsEU - C#
Рет қаралды 6 М.
Abstract Classes vs Interfaces (Java)
9:43
The Code Hard Truth
Рет қаралды 205 М.
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 198 М.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 164 М.
This Will Make Everyone Understand Golang Interfaces
21:03
Anthony GG
Рет қаралды 45 М.
Why I Chose Rust Over Zig
33:18
ThePrimeTime
Рет қаралды 44 М.
Inheritance vs Interfaces in C#: Object Oriented Programming
49:41
IAmTimCorey
Рет қаралды 101 М.
What you should know about Threads in .NET
14:52
Raw Coding
Рет қаралды 7 М.
ТАМАЕВ УНИЧТОЖИЛ CLS ВЕНГАЛБИ! Конфликт с Ахмедом?!
25:37