The distinction, use, and selection of abstract classes and interfaces

Source: Internet
Author: User

This paper mainly discusses the essential differences between abstract classes and interfaces in the semantic level, as well as the use and selection of abstract classes, which can have the implementation of such syntactic sugars.

First, Introduction

Abstract class, first is a class, class is the real World Object Modeling model, abstract class is the whole class of abstract description, including methods, and attributes. An interface is an abstraction of an attribute behavior of a class.

The inheritance of the abstract class is the is-a relationship, and the implementation of the interface is a "no" relationship. For example, birds and airplanes have the characteristics of flying, this time can be designed to fly this feature as an interface: IFly. Then let airplane and bird implement ifly this interface, so airplane and bird have the flight this attribute.

The next plane may have a variety of birds, the way they fly is completely different, so that Airplan and bird can be designed as abstract classes, so that different planes and birds inherit.

Class Diagram:

Code:

 Public Abstract classabstractairplane:ifly{ Public voidFly () { This.        Preparetofly ();  This.        Step1 ();  This.    STEP2 (); }    Private voidpreparetofly () {}protected Abstract voidStep1 (); protected Abstract voidStep2 ();}
 Public Abstract classabstractbird:ifly{ Public voidFly () { This.        Preparetofly ();  This.        Step1 ();  This.    STEP2 (); }    Private voidpreparetofly () {}protected Abstract voidStep1 (); protected Abstract voidStep2 ();}

In the above design, ifly defines the behavior of the fly:

    1. Abstractairplane is the implementation of this behavior, and defines the aircraft flight model, PREPARETOFLY,STEP1,STEP2. Since the STEP1 and STEP2 of different airplanes are not the same, they are defined as abstract, so that the subclasses implement their own
    2. Abstractbird implements this behavior, and defines the bird's flight model, PREPARETOFLY,STEP1,STEP2. Since the STEP1 and Step2 of different birds vary, they are defined as abstract, allowing subclasses to implement themselves

As can be seen from the above example, abstract class is a kind of model layout design, interface is a behavior specification.

Die Layout design: If the common part to modify, such as the preparetofly in, then only need to modify the common part, do not need to modify the other, for the abstract class, if you want to increase the method, you can directly manipulate the parent class, subclasses can not know.

Code of Conduct: If the interface is to be changed, the classes that implement the interface are modified.

The choice of interface and abstract class

In reality, there are two ways to open (), Close (), as defined in the following ways:

To define a gate using an interface method:

Interface Idoor    {        void  Open ();         void Close ();    }

To define a gate using an abstract class approach:

Abstract class Abstractdoor    {        publicabstractvoid  Open ();          Public Abstract void Close ();    }

At first all OK, but with the development of user needs, to the door to add alarm (Alarm) function, now how to do?

    1. Put alarm into the abstractdoor so that all inherited from this class door will have alarm function, but not all doors have alarm function, this violates the Liskov substitution principle
    2. Put alarm into the Idoor interface, need to alarm the door to implement this interface, then need to alarm the door to realize both open and close function, in fact, some alarm door does not need to open and close function,

Analysis:

As can be seen from the above, open and close are the intrinsic properties of a door, alarm belongs to the extension of the door, the best solution is to design an interface for alarm alone:

 Public Interface Ialarm     {        void  Alarm ();    }

The realization of ordinary door:

   Public class Normaldoor:abstractdoor    {        publicoverridevoid  Open ()        {        }        public Override void Close ()        {        }    }

The realization of the door with alarm function:

 Public class Alarmdoor:abstractdoor,ialarm    {        publicoverridevoid  Open ()        {        }        public Override void Close ()        {        }        publicvoid  Alarm ()        {        }    }

The implementation of a device that can only be alerted:

 Public class Alarmdevice:ialarm    {        publicvoid  Alarm ()        {        }    }

The distinction, use, and selection of abstract classes and interfaces

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.