📖 Crammy · All study guides
AP Computer Science A · Unit 9

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.

Study this unit free →

More AP Computer Science A guides

Inheritance
Subclass extends superclass; inherits fields/methods. Code reuse, hierarchy. class Dog extends Animal { }
Superclass vs Subclass
Superclass (parent): general class (Animal). Subclass (child): specific class (Dog). Subclass IS-A superclass.
Method Overriding
Subclass redefines superclass method. Same name/signature, different implementation. Enables polymorphism.
Method Overloading
Same method name, different parameter types/number. Different from overriding; both can coexist.
super Keyword
Access superclass methods/constructors. super.method() calls parent's method. super() calls parent's constructor.
Constructors & Inheritance
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.
Drill these as interactive flashcards →
Overriding Requires Same Signature
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.
Test yourself on this unit →
Constructor Chain Runs Upward First
Every subclass constructor invokes a superclass constructor before its own body, so the most general fields are initialized first.
Implicit super() Fails Without a No-arg
If the superclass declares only parameterized constructors, the subclass must call super(...) explicitly or the code will not compile.
Overriding vs Overloading
Overriding replaces a superclass method with the identical signature; overloading adds a different parameter list in the same or another class.
Covariant Return Types
An override may return a subtype of the original return type, which keeps the substitution guarantee intact.
Cannot Weaken Access in an Override
An override may widen visibility, so a protected method can become public, but narrowing it would break the superclass's promise.
super.method() Reuses Behavior
Calling the inherited version inside an override lets the subclass extend rather than replace, avoiding duplicated code.
Private Members Are Not Inherited
A subclass must reach superclass private fields through inherited public or protected accessors rather than directly.
Downcast Requires a Real Relationship
((Dog) animal).fetch() compiles for any Animal reference but throws ClassCastException at run time if the object is not actually a Dog.
Upcast Is Always Safe
Every Dog is an Animal, so assigning to a supertype variable needs no cast and loses only compile-time access to subclass-specific methods.
Polymorphic Collections
An ArrayList<Shape> can hold any subclass, and a single loop calling area() dispatches to each concrete implementation.
Abstract Method Forces an Override
Declaring a method abstract in a superclass means every concrete subclass must supply a body, guaranteeing the polymorphic call has a target.
Abstract Class Cannot Be Instantiated
It may still declare constructors and fields, which subclasses invoke through super, and it may contain fully implemented methods.
equals Parameter Must Be Object
Writing equals(MyClass other) creates an overload rather than an override, so collection methods will keep using the inherited identity comparison.
toString Inheritance Chain
A subclass override commonly calls super.toString() and appends its own fields, keeping the superclass description authoritative.
Single Inheritance of Classes
A Java class extends at most one superclass, which is why shared behavior across unrelated hierarchies is expressed with interfaces instead.
Is-a Test for Hierarchy Design
Inheritance is appropriate only when every instance of the subclass genuinely is an instance of the superclass in all contexts.
Substitution Expectation
Code written against the superclass must keep working when handed any subclass, so an override should not violate the documented contract.
Object Is the Universal Superclass
Every class ultimately inherits equals, toString, and getClass from Object, which is why any object can be stored in an Object variable.
Ask the AI tutor about this unit →
Field Access Is Not Polymorphic
Fields are resolved by the static type rather than the run-time object, so hiding a field with the same name in a subclass causes confusing behavior.
Turn these into flashcards & quizzes →