iterators

  1. .next() - gives you the next element, and advances the iterator, throws an exception if there is no next element.
  2. .hasNext() - true if there is, false if not

ex:

ArrayList<String> names = new ArrayList<>();

names.add("John");
names.add("Mary");
names.add("Rose");

Iterator<String> iter = names.iterator();

while(iter.hasNext()){
   String value = iter.next();
   System.out.println(value);
}


the programmer doesnt have to worry about iterating through the list


inheritance summary

Person p = new Student() but don't do Student p = new Person()


class hierarchy and Object class

Faculty carol = new Faculty("Carol Huffteacher", 
                           "999-99-9999", 1995);
Person p = carol;
System.out.println(carol.toString());

//which toString will be called?

the answer depends on the language you are writing code on, obviously in this class we are dealing with Java

Early (Static) binding - p is type Person, therefore it should call the Person's class toString()

Late (dynamic) binding - p refers to an Object of type Faculty so it should call the toString() of that class.

why do the creators of these languages choose between these two?

java uses late binding by default but it can be changed.

other languages like C++ use early binding (by default), for our example that means the Faculty.toString() is what is called.

polymorphism as it pertains to binding

ex:


Person[] list = new Person[3];
list[0] = new Person("Col. Mustard", "000-00-0000");
list[1] = new Student("Ms. Scarlet", "111-11-1111", 1998, 3.2);
list[2] = new Faculty("Prof. Plum", "222-22-2222", 1981);

for(int i = 0; i < list.length; i++){
  System.out.println(list[i].toString());
}

/*the toString() that gets printed is from that Object's class
*so the student will have their gpa printed out
*/

polymorphism (here) is the idea that the output of the code has many forms as in, it will output different things depending on the input. if it was early binding, you wouldn't print out the gpa or birthyear/admission for Student and Faculty


inheritance vs composition

according to the lecture video: when dont know whether or not to choose between composition or inheritance, pick composition because it is "easier to debug".