APCS Unit 8: Inheritance & Polymorphism

Creating new connections between classes

INHERITANCE & POLYMORPHISM OVERVIEW

Class structure and organization is a huge part of designing large programs.  Even last semester as we made multiple classes (like in our LHSZoo), it became apparent pretty quickly that the system we were working within was a little limited.  The reason for that though, is that we had not yet talked about inheritance or polymorphism.  In this unit we’ll learn a few new key words that let us do some cool things as we make classes connect to each other in better ways:

  1. Inheritance and the keyword extends will let us make subclasses of larger classes, granting variables and methods to subclasses immediately.
  2. Inheritance and the keyword super will let us talk from a subclass to a parent class, making the connections up the chain of inheritance possible.
  3. Polymorphism will even let us override inherited methods so that subclasses can be more specific about how they work.

When you’re done, you’ll have all the tools it takes to build complex class strucutres!

PART 1: INHERITANCE

(30 pts)  About 3 days

We start this unit be reviewing the basics of how to create (instantiate) an ArrayList as well as how to work with ArrayLists.  Here the focus will be the ArrayList constructor, as well as the methods add(), remove(), size(), and get().  Keep thinking about how ArrayLists are different from arrays! 

PART 2: POLYMORPHISM

(30 pts)  About 3 days

Next up we’ll focus in more detail on the differences between arrays (from Unit 5) and ArrayLists.  You’ll create various programs using arrays and then recreate them with arrayLists to consider the differences.  We’ll also look at when each of those tools is the right tool for the job!

PART 3: VIDEO GAME DESIGN

(20 pts)  About 3 days

Finally, we’ll return to some java challenges with ArrayLists just to get some more good practice.  Later in the semester we’ll look to make some more complex programs using ArrayLists.  For now, we’ll focus on getting a handle on the different tools we have at our disposal.

PART 1: Inheritance

Creating a multi-leveled class structure

Sample Inheritance Structure

Inheritance is a way for subclasses to be granted all the variables, methods, and constructors of their parent class simply by declaring that the class ‘extends’ the parent class.  The place this will be put in our program will be right in the class declaration. For example, the class declaration for the ForestAnimal class in the picture to the left would be:

public class ForestAnimal extends Animal

The extra “extends Animal” tells the program that all ForestAnimals are also Animals, and so they immediately inherit all the variables, methods, and constructors that Animals have… no extra typing necessary!  If we want ForestAnimals to have their own special ways of doing things, we’re still able to override the methods by simply rewriting them in the subclass.

In this first part of the unit, you’ll get to play with subclasses and get used to what the extends and super keywords can do for you.

GRADING & PROCESS

 Watch the three presentations on Inheritance and take careful notes in your engineering notebook.  Your notes should include details about how to use the keywords extends and super.

 Complete 6 Java Tasks about ArrayLists and traversing ArrayLists.

  Have Benshoof confirm your working programs! 

Inheritance 1

Inheritance 2

Inheritance Tutorial

What’s Due In Inheritance & Polymorphism Part 1: Inheritance

  • Inheritance Notes
  • 6 Inheritance Java Tasks
  • Benshoof’s Check-Off

Here’s what’s due in Part 1: ArrayList Basics

  1.  Take a page of notes on Inheritance, including details on the keywords “extends” and “super”
  2. Complete all 6 Inheritance Tasks in Java
  3.  Have Benshoof check-off your working programs

PART 2: Polymorphism

Using Interfaces to create class structures

One of the first questions that often comes up when we talk about inheritance and the idea of subclasses and superclasses is:  “can we extend multiple classes?”  The simple answer is ‘no’.  A subclass is only allowed to inherit methods and variables from one superclass.  This limitation helps keep some parts of our program in order, but admittedly keeps us from doing other things. 

Interfaces step in to help give us other functionality for ensuring that classes have specific methods defined, and also to help classes of different types all work in a similar way.  The best example of an interface that we’ve used (but never actually seen) is the comparable interface.  Classes that implement the comparable interface are allowed to be compared with standard <, >, and == symbols.  That’s been pretty handy, and it’s the comparable interface that lets us do that.  

In this part of the unit you’ll dig into writing interfaces (not difficult), and implementing interfaces within your abstract and concrete classes (also not too difficult).  We’ll build more class frameworks and hierarchies by adding in this new tool and thinking about what it might let us do!  

GRADING & PROCESS

 Take a full page of notes on Interfaces.  Make sure that your notes include details on what the “implements” keyword does, as well as how to actually implement an interface.  Also include in your notes some specifics about how abstract classes and interfaces differ!

 Complete 6 Java Tasks about interfaces!

 

Interfaces in Java

Connectable Interface Example

Abstract Classes vs Interfaces

What’s Due In Polymorphism & Inheritance Part 2: Polymorphism

  • Notes on Implementation
  • 6 Array Tasks
  • Benshoof’s Check-Off

Here’s what’s due in Part 2: Polymorphism

  1.  Take a full page of notes on on inheritance
  2.  Complete 6 Java tasks about inheritance
  3.  Have Benshoof check-off your working programs!

PART 3: Video Game Design

Practice your complete class structure in the design of a video game
Soldering – how to!  Give some thoughts and notes that summarize the big ideas
Check out this video on how to solder
Check out this video on how to wire your bugbot
Check out this extra video on troubleshooting

What’s Due In Bug Bot Part 3: Build It!

  • Build It
  • (Extra Credit) Decorate it
  • Benshoof’s Check-Off
Here’s what’s due in Part 3: Build It!

  1.  Get into the makerspace and build your Bug Bot!  Make sure it functions properly!
  2.  If you have extra time, give your Bug Bot that special touch by painting, lasering, or making stickers to customize it!
  3.  Have Benshoof check-off your finished, working Bug Bot

Double Check: Unit Expectations

Check what you need to have completed by the unit deadline
The purpose of this unit is to use the Engineering Design Process to wire, design, and build a working Bug Bot.  You should have had the chance to do some soldering, design your own Bug Bot, and use the tools in our makerspace to build your Bug Bot.  If you were really on-task and working hard, you should have also had time to decorate your Bug Bot to make it look super cool.  By the unit deadline, you should have completed and be able to submit the following items:

Engineering Notebook:

 (Part 1) Definition of problem, criteria, constraints, and Bug Bot Overview notes

 (Part 1) Wiring diagram, soldering notes, and wiring reflection

 (Part 2) Bug Bot Design brainstorm, notes, and careful drawing

 (Part 3) Final drawing/picture, half-page written reflection on Bug Bots

Building & Making:

 (Part 1) Wiring should work, soldering should be clean and complete

 (Part 2) Bug Bot design should be carefully drawn out including labels and diagrams

 (Part 3) The Bug Bot should be assembled and should function properly

Benshoof Checkpoints

 (Part 1) Benshoof should have seen your working wiring before you moved on

 (Part 2) Benshoof should have seen your careful drawing before you moved on

 (Part 3) Benshoof should have seen your working Bug Bot