Welcome to the eighth unit of AP Computer Science – we’re getting dangerously close to the end of our “new content” for the year, and that means we get to cover one of the coolest topics: Inheritance & Polymorphism! Object Oriented Design has made a lot of cool things possible so far this year, in particular the creation of our own classes, objects, and methods that can help organize larger programs. Here we get to look at how we can take advantage of inheritance structures using new keywoard in our classes. In fact, we’ll learn all these new keywords here:
This unit will have three main components as we look at more complex interactions between different classes:
- Inheritance – using the ‘extends’ keyword to create vertical inheritance relationships between classes. In addition, using the ‘super’ and ‘abstract’ keywords to define those relationships even more completely.
- Polymorphism – remembering that we can overload methods, learning what it means to overwrite methods, and putting together interfaces that will help create even more complex relationships!
- Video Game Design – planning out the structure of a larger video game using all the object oriented class design principles we’ve covered.
Example Class Structure
In the first part of our unit, we learn all about inheritance. Inheritance is the idea that classes can have more-specific sub classes of themselves. If we link classes using inheritance relationships, then when we make changes to one class, those changes are inherited downward through the hierarchy. This can make our programming job much easier while also giving us tools to make our programming job go faster! As we work, make sure you become comfortable with definitions for the following terms/keywords:
- Inheritance – making a class hierarchy using subclasses that are connected
- extends – the Java keyword that creates the inheritance relationship
- super – the Java keyword that lets the program look UP the inheritance chain by 1 level
- abstract – the Java keyword that makes levels of the inheritance hierarchy uninstantiable placeholders for the sake of organization
GRADING & PROCESS
Watch the videos on inheritance and take a full page of notes on those ideas. Make sure that your notes include detailed definions for the keywords listed above!
Work to complete the six Java Tasks on inheritance
Have Mr. Benshoof approve your completed Java Tasks
Classes vs Interfaces
Our next topic is called Polymorphism. The ideas of polymorphism are much simpler than the word suggests. Polymorphism is the idea that classes (and their corresponding objects) can have the same names and ideas, but different shapes. Something is polymorphic if there are multiple ways it can be used, or if there are multiple things that it can use. We’ve seen one half of polymorphism already when we overloaded constructors and methods. In this part of the unit we’ll see the other half where we learn about creating Interfaces and abstract classes.
In short, an INTERFACE is a list of methods that a class must know how to do. An ABSTRACT CLASS is a class in an inheritance hierarchy that cannot actually be constructed. We’ll see examples of all these throughout the unit.
Engineering Design Process Overview
The final part of unit 8 involves using what you know about inheritance (extends/super), class structures (abstract), and polymorphism (interfaces/implements) to create the structure for a large video game. As you design your video game class structure the goal is to show the heirarchy and is-a, has-a, and can-be-a relationships… not to create perfect classes with all the proper setters/getters/etc. Have some fun with it and think about what a larger program would need!
GRADING & PROCESS
Pick a video game you’ve played and know well (or a video game idea in your head that you’ve thought through already)
Make a plan in your engineering notebook for the general structure of your class hierarchy. brainstorm possible things (nouns) that could make your class structure complete
Create your entire game’s class structure and framework in BlueJ. It should be able to compile, but it doesn’t need to have too many complex variables or methods.
Write a full-page log about your knowledge and insights about inheritance and polymorphism
Part 1 Resources
Part 2 Resources