APCSA: Intro to Computer Science - Inheritance
Activity Goals
The goals of this activity are:
- To be able to explain the inheritance relationship among classes.
- To be able to inherit from a superclass.
- To be able to override a method from a superclass.
- To be able to call
super()
in a constructor or method.
- To be able to explain the difference between an interface, an abstract class, and a superclass.
- To be able to inherit
protected
methods or fields from a superclass.
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to take notes for the group, and appoint another member to discuss your findings with the class. Report out on areas of disagreement or items for which you and your group identified alternative approaches.
Model 1: Inheritance Relationship
Questions
- Which items in the model are "Vehicles?" Which are "Homes?"
- What kinds of things do a car, truck, and motor home have in common? What kinds of things do a motor home, townhouse, and apartment have in common?
Model 2: Interfaces
Questions
- Does an
interface
define fields, methods, both, or neither?
- What is the advantage of defining an
interface
?
- When using an
interface
, it is sometimes still necessary to duplicate code. Can you find an example of this in this Model?
- Run this example in repl.it. What must each file be named, and what code goes into which file? When you click Run, what
javac
command executes (specifically, what files are compiled)?
Embedded Code Environment
You can try out some code examples in this embedded development environment! To share this with someone else, first have one member of your group make a small change to the file, then click "Open in Repl.it". Log into your
Repl.it account (or create one if needed), and click the "Share" button at the top right. Note that some embedded Repl.it projects have multiple source files; you can see those by clicking the file icon on the left navigation bar of the embedded code frame. Share the link that opens up with your group members. Remember only to do this for partner/group activities!
Model 3: Inheriting from a Superclass, Abstract Methods, and super()
Questions
- How did each file change above by converting the
Vehicle
interface to a class?
- By converting the
Vehicle
interface to a class, what can we now define in a Vehicle
that we could not define in the interface
?
- You can implement an
interface
; what is the keyword to subclass a class?
- From context, what do you think the abstract keyword means in the
Vehicle
class?
- From context, what do you think the call to
super()
does?
- Notice that some elements are scoped to be
public
and private
, like before, but now some items are protected
. Which items are protected
, and in which files do they reside? What do you think this scope allows/disallows?
Try It with a Partner
Try It on Your Own
Submission
Report out on areas of disagreement or items for which you and your partner identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.
For Additional Practice
Feel free to visit these resources for additional practice exercises.