Inheritance: every key term you need (+ practice quiz)
55 flashcard terms for AP Computer Science A Unit 9, written to match the course framework. Read them here, drill them as flashcards, or take the 43-question quiz. Free, no account needed.
Subclass constructor must call super() to initialize superclass fields. super() must be first statement.
Access Modifiers & Inheritance
private: subclass can't access. protected: subclass can access. public: all can access. default: package only.
Abstract Classes
abstract class: can't instantiate directly. Forces subclasses to implement abstract methods. abstract method has no body.
Abstract Methods
Method with no implementation in superclass; subclass must override. Ensures all subclasses implement required behavior.
Polymorphism
One interface, many forms. Animal a = new Dog(); a.sound(); calls Dog's method, not Animal's. Dynamic dispatch.
Object Class
All classes extend Object implicitly. Methods: toString(), equals(), getClass(). Can override toString() for meaningful output.
Unit 9 Summary
Inheritance creates class hierarchy. Subclass extends superclass, overrides methods. Polymorphism via interfaces/abstract classes.
Implicit super() Call
If a subclass constructor does not call super(...) explicitly, Java inserts super() as its first line. If the superclass has no no-arg constructor, this is a compile error.
super(...) Must Be First
A call to a superclass constructor must be the first statement in the subclass constructor. Placing it later is a compile error.
Constructor Execution Order
Superclass constructor body runs to completion before the subclass constructor body, so inherited fields are initialized first.
Static vs Dynamic Type
Animal a = new Dog(); has static (declared) type Animal and dynamic (runtime) type Dog. The compiler checks calls against Animal; at runtime Dog's overrides execute.
Compile-time Method Check
You can call only methods declared in the static type. a.fetch() fails to compile if fetch is declared only in Dog, even though the object is a Dog.
Dynamic Dispatch (Late Binding)
When an overridden method is called through a superclass reference, the version belonging to the object's actual class runs. This is polymorphism in action.
An override must match name and parameter list; changing the parameters creates an overload instead, and the superclass version still runs through a superclass reference.
super.method() Call
Inside an override, super.method() runs the superclass version, letting a subclass extend rather than replace behavior.
Private Members Not Inherited
Subclasses cannot access a superclass's private fields directly; they use inherited public/protected getters or setters. Writing this.balance in a subclass when balance is private in the parent is a compile error.
Inherited Public Methods
A subclass inherits every public method of its parent, so a Dog object can call methods defined only in Animal without redeclaring them.
Downcasting
Dog d = (Dog) a; is allowed if a's runtime type is Dog or a subclass; otherwise ClassCastException. Casting is needed to call Dog-only methods through an Animal variable.
Upcasting is Automatic
Assigning a Dog to an Animal variable needs no cast because every Dog is-an Animal. Passing a Dog to a method with an Animal parameter is the same idea.
Polymorphic Arrays and Lists
Animal[] zoo = {new Dog(), new Cat()}; each zoo[i].speak() dispatches to the actual class's method. This is the standard AP polymorphism scenario.
Object Class Methods
Every class inherits equals(Object) and toString() from Object. If not overridden, toString prints ClassName@hash and equals compares references.
toString Inheritance
A subclass that does not override toString uses the parent's; a subclass override may call super.toString() and append its own fields.
Abstract Class Rules
Cannot be instantiated; may contain both abstract and concrete methods and constructors. A concrete subclass must implement all abstract methods or itself be abstract.
Interfaces (Beyond CED Scope)
An interface lists abstract methods that a class implements; the current CED tests only class inheritance, but the is-a reasoning is identical.
Method Hiding of static (Beyond CED)
Static methods are not polymorphic; a static method in a subclass hides rather than overrides. Not tested but explains odd outputs in code you may read.
Single Inheritance
A Java class extends exactly one superclass. Multi-level hierarchies (A → B → C) are fine; C inherits from both, and constructor calls chain up to Object.
Designing Hierarchies
Put shared fields and behavior in the superclass, specialized behavior in subclasses; the AP class-design FRQ often has you write a subclass with a constructor calling super and one override.
Liskov-style Substitution
Any code written for the superclass type should work when handed a subclass object. Overrides should keep the meaning of the method, just specialized.
Static Type Limits the Method Set
The compiler consults the declared type, so a variable of the superclass type cannot call a method that only the subclass declares.
Dynamic Type Chooses the Body
At run time the actual object's overriding version executes, which is what makes a single loop over a polymorphic list behave differently per element.
Two-step Method Resolution
Compile time checks that the method exists on the static type; run time selects which overridden implementation to invoke.