Hi,
Abstract Class:
An abstract class is a special kind of class that cannot be instantiated. So, why we need a class that cannot be instantiated? An abstract class is only to be inherited from. In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain common behavior or properties across multiple classes who inherit the abstract class.
Declaration:
public abstract class Employee
{
public virtual String CalculateSalary()
{
//Can provide default behavior
}
}
Interface:
An interface is an entity that is defined by the word Interface. An Interface only contains signature of the methods whose implementation is to be provided by the classes who implements that Interface.
Declaration:
public interface IEmployee
{
String CalculateSalary(); //Only Signature can be specified not even access modifiers.
}
Differance between abstract class and interface:
Abstract Class Interface
Multiple Inheritance Since .NET does not support multiple inheritance, A class can implement multiple Interfaces.
So A class can inherit only one Abstract Class
Default Behavior In abstract class you can provide default behavior of a You cannot provide a default behavior in interfaces.
function, so that even if child class does not provide its Interfaces only allow you to provide signature of the
own behavior, you do have a default behavior to work method.
with. Eg. Abstract classes of framework,
even if you don’t provide your own behavior,
they have a default behavior.
Access Modifiers You can access modifiers to the methods present in You cannot provide access modifiers to the methods
abstract class. present in interface.
Scenario
Now, lets talk about a single scenario that I hope it will make you understand the situations where to use what.
Lets Assume you need to make three classes, first is CAR, second is MAN, third is WOMAN. Now you need a function in each of them to define how they Move. Now all three can move but CAR moves entirely in different way than MAN and WOMAN. So here we use an Interface IMOVEMENT and declare a function MOVE in it. Now all three classes can inherit this interface. So the classes goes like this.
public interface IMovement
{
void Move();
}
public class Car : IMovement
{
public void Move()
{
//Provide Implementation
}
}
public class Man : IMovement
{
public void Move()
{
//Provide Implementation
}
}
public class Woman : IMovement
{
public void Move()
{
//Provide Implementation
}
}
But, since MAN and WOMAN walk in similar way, so providing same behavior in two different methods will be code redundancy, in simpler words code is not re-used. So we can now define a Abstract Class for Human Beings movements, so this class can be HUMANBEINGMOVEMENT. Also the same can be applied to CAR class, since there are lot of manufactures for cars and all cars move in similar way so we can also define a abstract class for Cars movement which can be CARSMOVEMENT. So our refactored code will be .
public interface IMovement
{
void Move();
}
public abstract class CarsMovement : IMovement
{
public virtual void Move()
{
//default behavior for cars movement
}
}
public class SuzukiCar : CarsMovement
{
public override void Move()
{
//Provide Implementation
}
}
public abstract class HumanBeingMovement : IMovement
{
public virtual void Move()
{
//default behavior for human being movement
}
}
public class Man : HumanBeingMovement
{
public override void Move()
{
//Provide Implementation
}
}
public class Woman : HumanBeingMovement
{
public override void Move()
{
//Provide Implementation
}
}
Now as you can see, an hierarchy is appearing now every human being can inherit from our HUMANBEINGMOVEMENT class for the move method and all types of cars can inherit from our CARSMOVEMENT class for the move method.
Mark as answer if satisfied...........
Regards,
Shree M.
Kavya Shree Mandapalli
krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator