Skip to content

4.21 Lecture 7

Introduction to Systems Programming - Lectures - Class 7.pdf

Interfaces

Revisiting types

A type defines a set of values, an what operations can be applied to those values.
The type itself doesn’t define the operations, it defines which operations can be applied to values of its type.
Examples: natural numbers, integer numbers, real
numbers, complex numbers, etc.


A type in OOP defines a set of values, and also the operations that can be applied to those values.
The type defines both the values, and their representation, and the operations, and their implementations.
Examples: LinkedList, ArrayList, etc.


An Abstract Data Type is a description of values and operations on those values, without defining the technical details on how those values are represented and how the operations work on those representations

Abstract Data Types

Lists

A List is a linearly organized collection of values, its main operations are:

  • Creation
  • Insertion/Deletion/Retrieval
  • Properties about a list: empty, size, contains.

Sets

A Set is an unordered collection of different elements, its main operations are:

  • Creation
  • Insertion/Deletion
  • Union/Intersection
  • Properties about a set: empty, size, contains, is a sub set.

Stacks

A Stack is a linearly organized collection of values (similar to a list), it’s a FILO collection (The First element you stack In, will become the Last one to come out), its main operations are:

  • Creation
  • Push/Pop
  • Properties about a list: empty, size

Queues

A Queue is a linearly organized collection of values (similar to a list), it’s a FIFO collection (The first element you First queue In, is the one which comes Out First), its main operations are:

  • Creation
  • Enqueue/Dequeue
  • Properties about a list: empty, size

Data Abstractions in Object Oriented Languages

image

  • Defines a new type
  • Operations (both declarations and definitions)

  • Create/Destroy Class values

  • Modify Class values
  • Check properties in Class values

These are called, objects, they are instances of a class.

Follow formal conventions, usually ClassName(arguments)

Data and operations are not separated, each object encapsulates both data and operations.

Usually memory management is automatic, we don’t need to destroy values


image

Abstract Data Types in OOPimage

image

image

Interfaces as Types

An interface in Java allows to define a new Abstract Type, while only providing a description but without providing a specific representation; and the operations applied to values of that Type, without providing any details on implementation.

  • Implementing classes are subtypes of the interface type.
  • So, polymorphism is available with interfaces as well as classes.

Interfaces as Specifications

  • Strong separation of functionality from implementation.

  • Though parameter and return types are mandated.

  • Clients interact independently of the implementation.

  • But clients can choose from alternative implementations.

Interfaces in Java

An interface in Java allows to define a new Type, while only providing a description but without providing a specific representation; and the operations applied to values of that Type, without providing any details on implementation.

  • An interface cannot have instances.
  • An interface cannot define/declare constructors.
  • An interface cannot define/declare fields (*).
  • An interface cannot define methods (*).
  • An interface can declare public methods (*).

(*) Several things have changed since Java 9, I recommend staying away from new features until the basic ones are fully understood.

Seeing a class through an Interface

image

image

image

image

image

image

Demo

Looking at LinkedList with different interfaces

It is possible for one class to implement several interfaces. We will start by looking at Java’s own LinkedList, and several interfaces implemented by it.

Making our own ADT and implementation

The implementation of an ADT might not be different than the implementation of another. A LinkedList can be used to implement Lists, Sets, Collections (*), Queues, Stacks, Deques, etc.

Function overloading

Several function names with different arguments

In a lot of cases, we have the same function/operation for different inputs (quantity or types).
For example, a max function: max(a, b) will return a if a is greater than b, according to some criteria.
Let’s explore some of these cases…

  • max(char, char)​, what should this do?
  • max(int, int)​, what should this do?
  • max(String, String)​, what should this do?
  • max(Student, Student)​, what should this do?
  • Could we abstract all of these using interfaces and generics?

Yes, but we need to provide the method of interface to compare two objects, classes,....

We have seen things like this before

image

image

This is an example of an operator overloading. But we could write this as a function/method.

This is in fact a very artificial example, we are abusing returning Object to be able to return anything as an Object.