Thank you i had 4 hours of sleep and i needed to get some more abstact and interface info into my brain, and as it is this weeks class subject and im trying to cram it in there.
for direct usage : for eg - Math m = new Math (); var r = m.round(3.12); // this does not make sense that is why we directly use the function like - abstraction Math.Round(3.12);
At th-cam.com/video/qgU2ojOKLP8/w-d-xo.html you say that in an abstract class we can always use virtual so even if Animal is not implementing the IAnimal interface, we can still use virtual on a method to have some default behaviour and the derived classes Dog and Cat do not have to implement MakeSound, they could implement it. But if we want to say that they have to implement it, then we have to make sure Animal implements IAnimal Interface. But with this too, if we have virtual MakeSound method in Animal class with default implementation, the derived classes work even without implementing MakeSound(). Why is this?
What do you mean? The whole point of virtual is to provide a default implementation in the parent class (that is abstract) and then inherit it in the child class to provide a different behavior for it. Abstract, on the other hand, just gives you a blueprint for a specific method or property that has no implementation in the parent class, but must have an implementation of the children that implement that class. Virtual does not enforce implementation either. Why? Because there is already default behavior in the parent class and that is sufficient for the compiler. The biggest difference with abstract classes and interfaces is the fact that abstract classes allow you to have base implementation in the parent class, whereas the interface cannot do that. Interface only serves are a plain blueprint which enforces other to implement all of the members of that interface. A good use case for an abstract class might be a class where you want to define generic SQL connection implementation for inserting or fetching generic records, which other classes (e.g. controllers) can make use of and do not constantly need to implement that logic in their own class. In other words, just defining functions that every derived class can use without having to to constantly make their own implementation thereof. So why use interfaces over abstract classes? Well, in C# you can only inherit from 1 class. Therefore, interfaces were created to ensure that classes can implement indefinite interfaces. If you look at languages like C++ and Python, they do not contain interfaces. Why? Because they allow for multiple inheritance, where you can just inherit as many times as you want. Why did C# do it like this? I have no idea. Most likely because this language is derived from Java, which also does not have support for multiple inheritance. Finally, come back to your question once again... Given that Animal class has default virtual implementation, why do the derived classes work without having the MakeSound() method? Well, because the parent class (Animal) already has an implementation for that class which does not enfornce the children classes to implement it. If you want this to be enfornced, just use the abstract key word in your abstract class.
🚀 C# Progress Academy - Become a job-ready C# and Angular web developer to land your dream developer role: bit.ly/45vbPUg
Thank you i had 4 hours of sleep and i needed to get some more abstact and interface info into my brain, and as it is this weeks class subject and im trying to cram it in there.
This one is a saver!!!
Thanks alot❤
Clarly explained. Thank you. Anyway, is this channel still active or do you move to the tutorialseu?
I think there is no need to define string as nullable object because it is already null by default if no value if given.
Why would you use abstract classes instead of interfaces?
for direct usage :
for eg -
Math m = new Math ();
var r = m.round(3.12); // this does not make sense
that is why we directly use the function like - abstraction
Math.Round(3.12);
Most of the examples on the internet don't explain it in the right way. That's why people always ask this question again and again. 😂
At th-cam.com/video/qgU2ojOKLP8/w-d-xo.html you say that in an abstract class we can always use virtual so even if Animal is not implementing the IAnimal interface, we can still use virtual on a method to have some default behaviour and the derived classes Dog and Cat do not have to implement MakeSound, they could implement it. But if we want to say that they have to implement it, then we have to make sure Animal implements IAnimal Interface. But with this too, if we have virtual MakeSound method in Animal class with default implementation, the derived classes work even without implementing MakeSound(). Why is this?
What do you mean?
The whole point of virtual is to provide a default implementation in the parent class (that is abstract) and then inherit it in the child class to provide a different behavior for it. Abstract, on the other hand, just gives you a blueprint for a specific method or property that has no implementation in the parent class, but must have an implementation of the children that implement that class. Virtual does not enforce implementation either. Why? Because there is already default behavior in the parent class and that is sufficient for the compiler.
The biggest difference with abstract classes and interfaces is the fact that abstract classes allow you to have base implementation in the parent class, whereas the interface cannot do that. Interface only serves are a plain blueprint which enforces other to implement all of the members of that interface. A good use case for an abstract class might be a class where you want to define generic SQL connection implementation for inserting or fetching generic records, which other classes (e.g. controllers) can make use of and do not constantly need to implement that logic in their own class. In other words, just defining functions that every derived class can use without having to to constantly make their own implementation thereof.
So why use interfaces over abstract classes? Well, in C# you can only inherit from 1 class. Therefore, interfaces were created to ensure that classes can implement indefinite interfaces. If you look at languages like C++ and Python, they do not contain interfaces. Why? Because they allow for multiple inheritance, where you can just inherit as many times as you want. Why did C# do it like this? I have no idea. Most likely because this language is derived from Java, which also does not have support for multiple inheritance.
Finally, come back to your question once again... Given that Animal class has default virtual implementation, why do the derived classes work without having the MakeSound() method? Well, because the parent class (Animal) already has an implementation for that class which does not enfornce the children classes to implement it. If you want this to be enfornced, just use the abstract key word in your abstract class.