Jump to content

Interface Vs Abstract Class


pr.nimbalkar

Recommended Posts

An abstract class defines method names and can also define some functionality.

public abstract class Soldier {  public abstract void Attack();  public void Defend() {	Console.Write("Soldier is defending");  }}public class Rifleman : Soldier {  public Rifleman() { }  public void Attack() {	Console.Write("Rifleman is attacking");  }}

So because Rifleman implements Soldier it must implement the abstract method Attack and also gets the Defend method because it was defined in the abstract class.PRO: You can define methods common to all class that derive from the abstract class.CON: A class can only implement from one abstract classAn Interface cannot define functionality. It can only define method/property names.

public interface ISoldier {  void Attack();  void Defend();}public interface IOfficer {  void CallAirStrike();}public class Rifleman : ISolider, IOfficer {  public Rifleman() { }  public void Attack() {	Console.Write("Rifleman is attacking");  }  public void Defend() {	Console.Write("Rifleman is defending");  }  public void CallAirStrike() {	Console.Write("Rifleman is calling an air strike");  }}

PRO: class can implment many interfacesCON: interface cannot define common functionalitySo it really depends on your situation. If you have common functionality then an abstract class is probably best. If you need to implement more than one contract then interfaces are the way to go. I like to use interfaces unless I have functionality I want to define in a base class.

Link to comment
Share on other sites

Hi, thanks for the answer! could you explain some more details with some different example??

An abstract class defines method names and can also define some functionality.
public abstract class Soldier {  public abstract void Attack();  public void Defend() {	Console.Write("Soldier is defending");  }}public class Rifleman : Soldier {  public Rifleman() { }  public void Attack() {	Console.Write("Rifleman is attacking");  }}

So because Rifleman implements Soldier it must implement the abstract method Attack and also gets the Defend method because it was defined in the abstract class.PRO: You can define methods common to all class that derive from the abstract class.CON: A class can only implement from one abstract classAn Interface cannot define functionality. It can only define method/property names.

public interface ISoldier {  void Attack();  void Defend();}public interface IOfficer {  void CallAirStrike();}public class Rifleman : ISolider, IOfficer {  public Rifleman() { }  public void Attack() {	Console.Write("Rifleman is attacking");  }  public void Defend() {	Console.Write("Rifleman is defending");  }  public void CallAirStrike() {	Console.Write("Rifleman is calling an air strike");  }}

PRO: class can implment many interfacesCON: interface cannot define common functionalitySo it really depends on your situation. If you have common functionality then an abstract class is probably best. If you need to implement more than one contract then interfaces are the way to go. I like to use interfaces unless I have functionality I want to define in a base class.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...