Abstraction
[comp1110-7Z] - Fabian Muehlboeck
Abstraction
[comp1110-7Z]- Fabian Muehlboeck
Construction 1. Interfaces
[comp1110-3L] - Liam O'Connor, Fabian Muehlboeck
Construction 1. Interfaces
[comp1110-3L]- Liam O'Connor, Fabian Muehlboeck
An interface is a reference type in Java that specifies a set of method signatures but does not provide their implementations.
It serves as a contract: any class that implements the interface promises to provide the logic for those methods.
An interface is typically open: any number of classes can implement it across different parts of a codebase.
You cannot create instances of interfaces directly, they must always be objects of a class implementing the interface.
Example 2. Generalizing Accounts
[comp1110-8A] - Fabian Muehlboeck
Example 2. Generalizing Accounts
[comp1110-8A]- Fabian Muehlboeck
Here is an interface describing the operations in Bank account class on a high level:
This matches the structure of the public methods of the BankAccount class in Bank account class.
That is a necessary, but not sufficient condition to be able to use Account to abstract over BankAccount.
In Java, you need to explicitly declare that this is your intention, by adding an implements declaration to the definition of the BankAccount class:
With this declaration, the Java compiler will check that the class has a public method matching each of the signatures in the interface.
We can now write a method that deals with any form of Account:
The above method takes an Account object, prints its balance, deposits 500, and prints the balance again.
This works fine with any BankAccount.
In addition, it will also work with other kinds of accounts, such as the following one modelling a frequent flyer account:
This class behaves differently from a BankAccount. It starts with an opening balance of 10000 as a "new member bonus".
In addition, whenever points are deposited, the member gets a 1The important thing here is that both kinds of accounts behave according the specification given by the Account interface, which only specifies preconditions and method signatures, both satisfied by both implementations.
Definition 3. Abstract Data Types
[comp1110-3P] - Liam O'Connor
Definition 3. Abstract Data Types
[comp1110-3P]- Liam O'Connor
Abstraction is the elimination of detail. In art, a realistic sketch of a bull captures every muscle and shadow, whereas an abstract version strips those away to convey the "bull" in a few lines. You lose specific texture but gain a universal representation.
In computer science, the interface is that line drawing. We eliminate the details of how data is stored and processed, instead defining a type, called an abstract data type or ADT, by the behaviour of the operations it supports alone. This separation of interface from implementation is a very powerful concept in programming. This allows the rest of the system to interact with the type abstractly, without regard to implementation details. This reduction of detail minimizes the mental load on the programmer and makes the code more flexible.
Construction 4. Inner Class
[comp1110-8B] - Fabian Muehlboeck
Construction 4. Inner Class
[comp1110-8B]- Fabian Muehlboeck
An inner class is a class defined within some other class, usually for the purpose of encapsulation.
Inner classes have access to the private members of the enclosing class, and vice versa.
Their objects must be constructed for a particular parent object of the enclosing class (this is implicit when creating the new instance from within an instance method of the parent class),
so that accessing fields of the parent class makes sense.
Inner classes are often private, because in most cases the form part of the inner representation of a given class.
They do not have to be, though in most cases where their objects would be access from outside of the parent class, they would still be private, but implement a public interface.
Example 5. The List ADT
[comp1110-8C] - Fabian Muehlboeck
Example 5. The List ADT
[comp1110-8C]- Fabian Muehlboeck
One of the most standard abstract data types is that of a List. A list is an ordered collection of items - ordered means that there is an order to the items in the list, that is, there is a first item, a second item, and so on. The list is not necessarily "sorted". Lists may contain duplicate values. One can typically query the element at a particular index in the list, add elements to a list, remove elements from a list, and get the length of a list.
There are in fact multiple levels of abstraction. The above description applies to pretty much anything a computer scientist would call a "list". The ability to change the size of the list by adding or removing elements is the key thing that distinguishes lists from arrays (lists can be, and in many cases are, implemented using arrays). But the description is at a very high level, and leaves out certain details that vary a lot between different libraries and programming languages, for example whether lists are mutable entities or the above abstract operations construct a new, slightly changed copy of the original list every time.
In Java, the typical choice is for things to be mutable, and so a standard list interface might look as follows:
This interface describes the above basic operations.
Using void as the return type of the add and remove operations indicates that it is the particular list object itself that is changing when those methods are called.
A more "functional" version of this code would return a new List object for those methods.
There are multiple ways to implement this interface. Here, we choose a linked list.
The above class implements our List interface. It uses an inner class to have a linked list implementation inside - the advantage for the user here is that they do not need to distinguish the base case from the recursive case themselves anymore, the list implementation does this for them. The methods in the class are implemented with a mix of recursion and iteration for demonstration purposes. In general, one should use iteration here in Java.
One can now write code using just the List interface.
The only point at which the LinkedList class comes in is when the particular list object is constructed, though in that place, it could also be replaced with any other class implementing the List interface.