Little drops on C# Interview Questions series
This is the first drop, the intentions behind this series are to answer with a little bit of context or examples some of most commons C# interview questions.
Abstract classes x Interfaces
In order to resume you can say that an interface allows you to define functionality and abstract class allows you to create some behavior that the subclass will implement or override.
But there's more than that of course.
Abstract classes
According to the documentation: "The abstract modifier indicates that the thing being modified has a missing or incomplete implementation."
Okay, so what this means.
Basically means that an abstract class can be partially implemented or not implemented at all, its purpose is to act as a base for other classes.
Interfaces
According to the documentation: "An interface contains definitions for a group of related functionalities that a non-abstract class or a struct must implement."
You can see an interface as a contract that you must implement and cannot contain methods definitions or constructors, the methods declared here have to be implemented on the classes that implement the interface and the interface cannot be instantiated.
When to use on or other?
Abstract classes for having some concretes methods are great to create common code shared between classes acting as a base class.
On the other hand, interfaces are easy to define contracts and behaviors that the classes will have to implement like in dependency injection, also you can add as many interfaces as you wish to the hierarchy which cant be done with abstract classes.
Quick demo
I this little showdown we have 3 classes and an interface
First, the base class has two properties that will be shared between Rick and Morty and two methods that both can use!
public abstract class Base
{
public string Name {get; set;}
public string Gun {get; set;}
public static bool wantsToStartNewAdventure()
{
return true;
}
public static bool thinksMortyIsStupid()
{
return true;
}
}
The interface obligates both to implement usage for the portal gun.
public interface IPortalGun
{
public bool UsesPortalGun();
}
Rick takes the properties and from the base, he thinksMortyIsStupid, also implements the interface bragging to Morty and using the portal gun!
public class Rick : Base, IPortalGun
{
public Rick() : base(){ }
bool ThinksMortyIsStupid = thinksMortyIsStupid();
public bool UsesPortalGun()
{
if(ThinksMortyIsStupid)
Console.WriteLine("Look at me Mortyyy im a demo c# class!!");
return true;
}
}
And finally, Morty takes from the base the properties and the wantsToStartNewAdventure method also implements the gun portal usage telling rick he can't use it!
public class Morty : Base, IPortalGun
{
public Morty() : base() { }
bool isReadyForAdventure = wantsToStartNewAdventure();
public bool UsesPortalGun()
{
if(isReadyForAdventure)
Console.WriteLine("Ooh geez Rick i cant use this, but lets go for and adventure");
return false;
}
}
This is it, guys! Hope it helps feel free to reach out to me with comments and feedback.
See you in the next one.
Top comments (1)
Nice work man!