Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interfaces are like contracts. If any part of your code should depend on the presence of some methods or properties, an interface is a good idea. It also provides more space for unit testing.</p> <p>Many times I define both an interface and abstract class implementing it. This way, you can have an implementation of the interface without deriving from the base class.</p> <p>As for real word example, consider a message gateway, for example. <em>Please, note that the following implementation is not that OOP-perfect. I just didn't want to create so many classes and interfaces.</em></p> <pre><code>interface IMessageSender { string From { get; set; } string To { get; set; } string Message { get; set; } void Send(); } abstract class MessageSenderWithSubjectBase : IMessageSender { string From { get; set; } string To { get; set; } string Message { get; set; } string Subject { get; set; } abstract void Send(); } class EmailSender : MessageSenderWithSubjectBase { override void Send() { // send email } } class SmsSender : IMessageSender { override void Send() { // send sms } } </code></pre> <p>See, an SMSes does not have a subject. You could derive from the abstract class as well and just ignore the subject, but that is not a clear design. Not to mention situation when there are common method in a base class you know that you don't need at all. Instead you can create a base class for messages without subject or just implement the the interface.</p> <p>Somewhere in code when you will need to send a message, you will probably get a message sender from some sort of a factory and you can rely that it will be able to send your message because it implements the interface. You can be abstract like that.</p> <p>Although this answer does not directly answer your question, it is because I don't think that you can create rules like those you have read. Given a time and many lines of code and you will eventually understand when you need interface, abstract class or both simultaneously.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload