2016-12-01

posted Dec 1, 2016, 6:01 AM by Samuel Konstantinovich   [ updated Dec 1, 2016, 1:02 PM ]

Interface recap:


Writing Interfaces:


Writing interfaces are similar to writing classes. They are their own separate file and instead of writing “public class className”, you write “public interface InterfaceName”. The methods should only call its type, name, and parameters.

eg.


public interface InterfaceName{

String method1(String param1);

//this is a method signature, just like abstract method signatures

int method2(double param2);

}

Interfaces only have constants and method signatures (NOT method bodies)

Technically they can contain only:
constants, 
method signatures, 
default methods, 
static methods,  
nested types.  (we didn't learn this yet)

When to use Interfaces vs Inheritance?
When we talk about abstract classes we are defining characteristics of an object type, specifying what an object is. You can only extend from one base class so you want to inherit from the class which most accurately defines what you are.

In the case of an interface, we define a capability, we are talking about specifying what the object can do. You can implement as many interfaces as you need. 


ENHANCED FOR LOOP!




Another Interface: The Iterable Interface

  • Iterable means it returns an iterator when it calls the iterator() method.

    • For example, an array is iterable, it can be iterated through.

  • Iterable has one method: Iterator() which returns an iterator for a set of elements of type T

    • This can be used, for example, in an Enhanced For loop:

for(T a : c){ //where c is the iterator, and a is of type T

//do something to a

}


for(Integer i : listOfStuff){
//where listOfStuff is an ArrayList for example

//do something to i

}

  • An iterator is basically something that has a “Next” (So while there is another thing, keep going; it allows you to go through every item in something)

  • For example, in iterator can be made for a set of numbers, let’s say between 0 and 500000000:

    • for(int i: new IntegerIteratorThing(500000000)){}

      • where IntegerIteratorThing iterates through some number up to it, or whatever else you’d like

  • It can be made for any collection of things.

  • The enhanced for loops requires an Iterator. Any class that implements Iterable, can create an iterator. So only Iterable classes can use the enhanced for loop.



Code from class. I modified it to be 3 separate files, so the
Demo runs everything. It loops over a ContinuousSetOfIntegers.
ContinuousSetOfIntegers is a data structure that stores a start and end. It is iterable.
IntIterator is the iterator, it is only used by the ContinuousSetOfIntegers.

//DEMO!/////////////////////////////////////////////////////////////////////////////////

import java.util.*;
public class Demo{

  public static void main(String[]args){
      ArrayList<Integer> nums = new ArrayList<Integer>();
      int[]nums2= new int[10];

      for(int i = 0; i < nums2.length; i++){
        nums2[i]=(int)(Math.random()*100);
        nums.add(new Integer(nums2[i]));
      }

      System.out.println(nums);

      System.out.println(nums2);

      //standard for loop
      for(int index = 0; index < nums.size(); index++){
        System.out.print(nums.get(index)+" ");
      }
      System.out.println();

      //enhanced for loop on ArrayList
      //int or Integer
      for(Integer v : nums){
        System.out.print(v+" ");
      }
      System.out.println();

      //enhanced for loop on array
      for(int v : nums2){
        System.out.print(v+" ");
      }
      System.out.println();

      IntIterator mystery = new IntIterator(5,10);
      while(mystery.hasNext()){
        int v =mystery.next();
        //body
        System.out.println(v);
      }

      ContinuousSetOfInts stuff = new ContinuousSetOfInts (1,8);
      for(Integer v : stuff){
        System.out.println(v);
      }
  }
}

//The thing that you loop over, and can manufacture an iterator:
///////////////////////////////////////////////////////////////////

import java.util.*;
public class ContinuousSetOfInts implements Iterable<Integer>{
  private int start,end;

  public ContinuousSetOfInts (int start,int end){
    this.start = start;
    this.end = end;
  }

  public Iterator<Integer> iterator(){
    return new IntIterator(start,end);
  }
}





//The thing that iterates://///////////////////////////////////////////////////////////////////

import java.util.*;
public class IntIterator implements Iterator<Integer>{
  int start,end;

  public IntIterator(int start, int end){
    this.start = start;
    this.end = end;
  }

  public Integer next(){
    if(hasNext()){
      start++;
      return start-1;
    }else{
      throw new NoSuchElementException();
    }
  }

  public boolean hasNext(){
    return start < end;
  }

  public void remove(){
    throw new UnsupportedOperationException();
  }

}

API • 

Comments