Ultimate Sidebar

The Difference Between Interface & Abstract Class

104 2

    Class Models

    • Both interfaces and abstract classes serve as a kind of template for a particular class. Programmers create these classes in order to specify certain generic behaviors for the class, and allow the individual programmer who ends up using the class to specify the behaviors of the class for the programmer's particular application. While they both act as general models, they have different protocols for declarations and impose different requirements on the end programmer.

    Class Declarations and Implementations

    • Programmers specify if a class is an interface or an abstract class with different keywords in the class declaration. This will be either "interface" followed by the name of the interface, or "abstract class" followed by the class' name. Programmers using either the abstract class or interface don't use the class directly, but create a sub-class of the particular class type they will be using. To use an interface, a programmer would add "implements interface" where "interface" is the name of the particular interface. To use an abstract class, a programmer would add "extends abstractClass" where "abstractClass" is the name of the abstract class.

    Method Implementations

    • Subclasses can override methods in their parent class (in this case the interface or abstract class). When a programmer declares a method by the same name as a method in the parent class, then the program will use the code for the method which the user provides instead of the code in the parent method. In abstract classes, some methods will be declared as "abstract." These methods deal with the specifics of how a class will be used, and the end programmer has to over-ride the methods which are declared as abstract. In interfaces, the end user has to over-ride every method in the interface.

    Situational Use

    • These functional differences lend themselves to specific uses. Using an interface ensures that a particular class will meet all the requirements for a particular data structure. For example, a programmer who made his own library of network connection functions can make programmers use and interface class he designed for data that the end programmer wants to send using those functions. The interface will ensure that every outgoing transmission will have everything the libraries need in order to successfully send that data. Alternatively, that same programmer could make an abstract class which contained methods that detailed the nuts and bolts of sending the data, but required the end programmer to over-ride certain methods dealing with where the data would be sent or what would be sent.

Source: ...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.