Unit 9: Inheritance

AP Computer Science A: 48 practice questions with detailed explanations.

Unit Study Guide

Executive Summary

Inheritance lets a subclass reuse a superclass's public members and override methods to specialize behavior.

Extending classes

class Dog extends Animal means Dog is-a Animal. The subclass inherits public methods and variables but NOT private members. Every class extends Object, gaining toString() and equals().

Overriding vs overloading

Overriding redefines an inherited method with the SAME signature; the subclass version runs for subclass objects. Overloading is same name, different parameters, in the same class. An override cannot reduce access (public to private fails).

super

super.method() calls the parent's version from inside an override. super(args) must be the first statement of a constructor; if omitted, Java inserts super() automatically.

Polymorphism

Animal a = new Dog() is legal upcasting. Calling a.speak() dispatches to Dog's version at runtime (dynamic dispatch). What you can CALL is limited by the reference type (Animal), but what RUNS is chosen by the object's class. Static methods, by contrast, bind to the declared type.

Exam traps

A superclass reference cannot call subclass-only methods. Constructors run parent-first. private members are not inherited. Final classes cannot be extended.

Top 5 Concepts to Master

  1. 1Predict which method version runs.
  2. 2Write constructors that call super correctly.
  3. 3Explain is-a relationships and upcasting.
  4. 4Trace polymorphic method calls.

Key Terms & Definitions

Practice with Flashcards
extends

Keyword declaring a superclass.

Override

Redefining an inherited method, same signature.

super

Reference to the superclass method or constructor.

Polymorphism

Runtime dispatch choosing the object’s method.

Upcasting

Storing a subclass object in a superclass reference.

Object class

Root of the hierarchy; source of toString/equals.

Dynamic dispatch

Method chosen at runtime by object class.

Common Misconceptions: Exam Traps

Private superclass members are inherited.

Correct: Subclasses cannot access them directly.

super must be written in every constructor.

Correct: It is implicit when omitted, calling the no-arg parent.

A superclass reference can call any subclass method.

Correct: The reference type limits which methods are callable.

Static methods dispatch by runtime type.

Correct: They bind to the declared reference type.

Question Bank Breakdown

By difficulty

easy 20medium 25hard 3

By topic

Superclasses and Subclasses 14Polymorphism 11super Keyword 8Object class 8Overriding Methods 7

All Questions in this Unit