Recommendations for Abstract Classes and Interfaces

I know the difference between an interface and an abstract class. Now I would like to know exactly where I need to use the interface over the abstract class, vice versa.

In the article I called Recommendations for Abstract Classes vs. Interfaces

In that

Here are some guidelines to help you decide whether to use an interface or abstract class to provide polymorphism for your components.

  • If you expect multiple versions of your component to be created, create an abstract class. Abstract classes provide a simple and easy way to version your components. When updating the base class, all inheriting classes are automatically updated with the change. Interfaces on the other hand, cannot be changed after creation. If a new version requires an interface, you must create a completely new interface.

  • If the functionality you create is useful for a wide range of disparate objects, use the interface. Abstract classes should be used primarily for objects that are closely related, while interfaces are best suited to provide common functionality to unrelated classes.

  • If you are developing small, concise pieces of functionality, use interfaces. If you are developing large function blocks, use an abstract class.

  • If you want to provide common, implemented functions among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, while interfaces do not contain implementations for any members.

I'm not sure about point number 3. Do I need to put all the small functions of my component in the interface?

/ , ?

+4
5

, , " " . A B C, - - , ? C doSomething(), C ( C , A B doSomething()).

, , №3, , , . "" , , . ( , , , , API.)

+1

, , , IMovable, , , IMovable Move, , , , , , . http://dofactory.com/Patterns/PatternAbstract.aspx#_self2, , , , , , .
.

+1

. - , . .

, , . , .

, , , . , , OOP. .

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 }
}

., SMS- . , . , , , , . .

- , , , , factory, , , . , .

, , , , , . , , , .

+1

(3).

, , . , .

, . provider, Validate .

0

, , , , , , ? , , . , , , , , , ?

Java ( , ), . , .

, . , , . , superClassOrInterfaceType ( , . , ) , , , , superClassOrInterfaceType, , subtypeClassOrInterfaceTypes, : , , . , superClassOrInterfaceType subtypeClassOrInterfaceTypes.

, superClassOrInterfaceType , . , subtypeClassOrInterfaceTypes. , , , superClassOrInterfaceType , . , , , . SubClipOrInterfaceTypes, .

, , , , . , .

, , , , . , , , . : Java . , .

To summarize, you usually want to define the behavior (what the objects will do) with interfaces, and not in abstract classes. Abstract classes focus on implementation hierarchies and code reuse.

Here are some links that are more relevant to this issue.

Thanks Type and Gentle Class

The magic behind subtype polymorphism

Maximize flexibility with interfaces and abstract classes

Interfaces against abstract classes in Java

0
source

All Articles