Force students to explore inheritance and the Java APIs for ArrayList by writing code that HAS-A ArrayList or IS-A ArrayList.

  • Students often don’t understand the motivation for Java inheritance and also have difficulty navigating Java API pages.
    • You can encourage students to explore the Java API using either of the two activities:
      • Activity A: Have students write a Java Class that HAS-A ArrayList
      • Activity B: Have students write a Java Class that IS-A ArrayList
  • Activity A: HAS-AN ArrayList
    • Assign students to write a class that has a Java ArrayList
      • This goal is to encourage them to explore Java APIs and call provided methods.
    • Ask students to look up available methods in java documentation for ArrayList and use those methods to implement the library methods
      • For example, you could have students write code to show them that you can build on all of the functionality of the Java library classes like the code shown below.
        • import java.util.ArrayList;
          public class Library{
              public ArrayList books;
              public void addBook(Book b){
                  books.add(b);
              }
          }
  • Activity B: IS-AN ArrayList
    • Assign students to create a class that extends Java ArrayList to encourage them to explore Java APIs and call provided methods.
      • For example, you could have students write code like the code shown below to show students that you can build on all of the functionality of the parent class!
        • import java.util.ArrayList;
          public class Library extends ArrayList {
              public void add(Book b) {
                  super.add(b); // calling the ArrayList method
              }
          }
      • Note from CS Teaching Tips Team: This has the unfortunate consequence that every public method provided by ArrayList is also available for your new class, which is probably not something you’d want.

More about this tip

External Source

Interview with Elissa Redmiles.