CS62 - Fall 2026 - Class 7

Example code in this lecture

   Interfaces
   ArrayList

Lecture notes

  • admin
       - Darwin part 1 (World and Species) due tonight
       - Rest of Darwin due next Tuesday
       - Quiz tomorrow in lab

  • interfaces
       - There are two main ways we can indicate shared behavior. The first is through inheritance.
       - The second is through interfaces. Interfaces define methods that a class must define. E.g.,
          public interface InterfaceName{
             public void method1();
             public int method2(int number);
             public ArrayList<String> anotherMethod(String myString);
          }

          (see ExampleInterface in Interfaces code)

       - A class can then "implement" these methods by adding "implements" in the class definition:

          public class A implements InterfaceName{

          }

          - we must then implement ALL of the methods described in the interface, or our code will not compile

       - Why might this be useful?
          - interfaces define a type, i.e., we can declare variables and parameters of that type
          - allows us to group objects together with similar functionality
             - public void example(InterfaceName obj){...}
             - we know that we can call any methods defined in the interface from that object
          - we can say, the only thing that's important to me is that the class implement method x, y and z

  • look at the SimpleInterface in Interfaces code
       - two methods

  • look at the NegativeNumber and PositiveNumber classes in Interfaces code
       - Two classes that both implement the SimpleInterface interface
       - Notice that if I don't include one of the methods in the class, Java will complain
       - I can also have other methods

  • look at the NumberUser class in Interfaces code
       - Like a class, interfaces define a type
       - The methods that we can call on variables/parameters of that type are those defined by the interface
       - Any object that implements that interface can be put into a variable/parameter of the interface type
       - in the main method, we can call the useNumbers method with either a NegativeNumber object *or* a PositiveNumber object.

  • look at the BasicList interface in Interfaces code
       - defines a very basic interface for lists
          - get/set
          - add (add to the end)
          - size

       - like classes, we can also use generics on interfaces


  • ArrayLists
       - Why do you think its called "ArrayList"?
          - the underlying structure that it stores the data in is an array

  • to the slides!

  • look at the BasicArraylist class in ArrayList code
       - keep track of two instance variables
          - data (the actual data)
          - size (the number of elements in the data)
             - note this is different than the size of the data array
       
       - Anything new/unusual in the constructor?
          - we cannot create new arrays of type E (or of any type variable)
             - this has to do with how Java implemented generics
          - instead, we create a new array of Objects and type cast it to an E[]

       - look at get and set
          - what do you think the rangeCheck method does?
             - checks to see that the index is within a valid range

       - look at rangeCheck
          - if we want to have a method raise an exception, we use the keyword "throw"
             - remember, exceptions are just classes so we need to create a new instance

  • what are the run times of get, set, and size?
       - Assuming array index operations are O(1), then O(1)

  • adding to an ArrayList
       - so far, we haven't added any functionality over arrays
       
       - what was the main functionality that ArrayLists added over arrays?
          - add/extend

       - how do you think this is done?
          - we allocate some initial array
          - we fill it in as we go
          - what happens when it fills up?
             - we have to create a bigger array!
             - copy over the old values

  • what does the resize method do in the BasicArrayList class in ArrayList code ?
          - creates a new array of type E
          - copies the existing value
          - then sets data to be that new array!

       - look at add method
          - check to see if we're out of space
             - if so, resize
             - we can choose any size that's larger than the existing
             - we'll talk more about how different sizes might affect things