What is an interface and why are they useful?

An interface in the Object Oriented Programming (OOP) sense is very similar to a class definition - it is the definition of the various functions and procedures provided by the class (in this case interface), with no actual code. For example, an interface for a list data structure would include procedures such as "append" or "add" which are used to add some data to the end of the list, along with procedures such as "get" to get data from a certain index and "remove" to get rid of a piece of data. The interface would only list those procedures, and not provide any of the code that actually carries them out (the implementation). An implementation of the list, for example a linked list, could then be written that inherits from the interface, which means that it is required to implement every procedure in the interface, and that it counts as a "list" object, as well as a "linked list" object. There are two main reasons that interfaces are useful. The first is abstraction - the interface hides the actual implementation when we don't need to know about it. This is useful because if I'm writing a program that uses a list, I don't care about how the list is implemented (as a linked list, or with arrays, or otherwise), I just need to know what I can do with a list. An interface provides an easily readable description of what I can do with a list, without needing to know how the list is actually implemented, which simplifies my code and is especially useful in larger projects. The second reason interfaces are useful is that they provide the ability to change implementations without breaking your program. This is useful because some implementations are better than others in different situations. In the list example, a linked list is very fast for adding items to the end, and for reading it in order, but is very slow at reading from random locations. An array list is very fast for reading from random locations, but slow for adding new items. If I wasn't using an inteface, changing between these would require rewriting all the code that uses the list. But if I use the list interface, I can just switch the implementation and leave my code untouched.

Answered by Kajetan C. Computing tutor

6667 Views

See similar Computing A Level tutors

Related Computing A Level answers

All answers ▸

How do I solve a Karnaugh Map?


Explain why it is important to check the email address with JavaScript and again when it reaches the server.


How does a HDD (Hard drive disk) work?


Given a graph with n nodes and m edges, every edge has a passing cost that can be negative, find the minimum distance between node 1 and every other node


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy