Unit 9: Inheritance

Superclasses, subclasses, method overriding, polymorphism, and Object class

Unit Resources

Select a resource below to start studying.

📚Study Guide: Inheritance

Unit 9: Inheritance

Inheritance is a cornerstone of object-oriented programming that enables code reuse and the creation of hierarchical relationships among classes. This unit covers extending classes, overriding methods, polymorphism, and the use of abstract classes and interfaces. When a subclass extends a superclass, it inherits all public and protected fields and methods. The subclass can override inherited methods to provide specialized behavior, and the @Override annotation helps catch errors at compile time. The super keyword allows a subclass to call the superclass's constructor (super(...)) or methods (super.methodName()). Polymorphism means that a superclass reference can point to a subclass object, and method calls are resolved at runtime based on the actual object's class (dynamic binding). The AP exam heavily tests these concepts, often presenting class hierarchies and asking students to predict output based on reference types versus object types. Abstract classes cannot be instantiated and may contain abstract methods that subclasses must implement. Interfaces define a contract of methods that implementing classes must provide, enabling a form of multiple inheritance in Java. Understanding the difference between compile-time type (reference) and runtime type (object) is critical for determining which method executes and which methods are accessible.

Key Concepts

  • Class Extension: public class Dog extends Animal { ... } Dog inherits accessible members of Animal. Private members are not inherited directly but can be accessed via public/protected methods.
  • Method Overriding: A subclass redefines a superclass method with the SAME signature (name and parameter types). Use @Override to ensure correctness. The return type must be the same or a covariant subtype.
  • Polymorphism: Animal a = new Dog(); // valid. The reference type is Animal; the object type is Dog. a.speak() calls Dog's speak() if overridden (dynamic binding). However, a can only call methods declared in Animal unless cast to Dog.
  • super Keyword: super(params) calls the superclass constructor and must be the first statement in the subclass constructor. super.method() calls the superclass version of an overridden method.
  • Abstract Classes: Declared with abstract. Cannot be instantiated. May contain abstract methods (no body) that subclasses MUST implement. Can also contain concrete methods and fields.
  • Interfaces: Declared with interface. Contains only method signatures (and default/static methods in Java 8+). A class implements an interface using implements. A class can implement multiple interfaces.

Vocabulary

  • Superclass (Parent Class): The class being extended; provides inherited fields and methods.
  • Subclass (Child Class): The class that extends another class, inheriting its members and potentially overriding methods.
  • Dynamic Binding (Late Binding): The process by which a method call is resolved at runtime based on the actual object's class rather than the reference type.
  • Covariant Return Type: When an overridden method returns a subtype of the return type declared in the original method.
  • Abstract Method: A method declared without an implementation in an abstract class or interface; must be implemented by concrete subclasses.
  • Casting: Explicitly converting a reference from one type to another; downcasting (superclass to subclass) may cause ClassCastException at runtime if invalid.

Essential Formulas / Syntax Patterns

  • public class Cat extends Animal { @Override public void speak() { ... } }
  • Animal a = new Cat(); a.speak(); // calls Cat's speak
  • super(name); // calls Animal constructor
  • public interface Drawable { void draw(); }
  • public class Circle implements Drawable { public void draw() { ... } }

Common Mistakes

  • Confusing Overloading and Overriding: Overloading uses different parameters in the same class. Overriding uses the SAME signature in a subclass.
  • Calling Private Methods Polymorphically: Private methods are not inherited and cannot be overridden. If a subclass defines a method with the same name, it is a new method, not an override.
  • Trying to Instantiate Abstract Classes: new AbstractClass() causes a compile-time error. You can only instantiate concrete subclasses.
  • Illegal Downcasting: Animal a = new Dog(); Cat c = (Cat) a; compiles but throws ClassCastException at runtime because a Dog is not a Cat.

AP Exam Strategies

  • Trace Reference vs. Object Type: When given Animal a = new Dog();, ask: What methods are available? (Animal's). Which method executes? (Dog's overridden version). Can I cast? (Only to Dog or its subclasses).
  • Use @Override: Always include @Override when overriding. It catches signature mismatches at compile time.
  • Draw Class Diagrams: Sketch arrows from subclass to superclass and label overridden methods. This clarifies polymorphic behavior.
  • Understand Constructors in Inheritance: The superclass constructor always runs before the subclass constructor. If the superclass has no default constructor, the subclass MUST explicitly call super(...).

Real-World Applications

  • GUI Frameworks: JavaFX and Swing use inheritance extensively; JFrame extends Frame, JButton extends AbstractButton, and event listeners implement interfaces.
  • Game Engines: A base Entity class is extended by Player, Enemy, and Projectile, each overriding update() and render() for specialized behavior.
  • Plugin Architectures: Applications define interfaces (e.g., PaymentProcessor) that third-party plugins implement, enabling polymorphic integration of new features.

Practice Quiz: Inheritance

Answer each question one at a time. Click an option to select your answer.

Question 1 of 150
Question
Loading...
Click to flip
Answer
Loading...
Click to flip back 🔀 Shuffle
1 / 43

🎥Free Video Lessons: Inheritance

Watch these unit review videos directly on our site.

AP Computer Science A Unit 9 Review - Inheritance - Super & Sub Class - Methods - Constructors - CSA by Meek Extra Help

APCSA Unit 9 Review by Brian Moore

APCS Unit 9 (Part 1): Inheritance In-Depth Review and Practice Test by AP Computer Science A

📄Cheat Sheet: Inheritance

Quick reference for Inheritance. Print this out and review before the exam!

Unit 9: Inheritance - Cheat Sheet

Key Keywords

  • extends -> class inheritance
  • implements -> interface implementation
  • super -> call parent constructor/method
  • @Override -> override parent method

Polymorphism Rules

  • Reference type determines what methods are callable
  • Object type determines which overridden method runs
  • Animal a = new Dog(); a.speak() -> Dog's speak

Abstract Class vs. Interface

  • Abstract: Can have fields, constructors, concrete and abstract methods. Extended with extends.
  • Interface: Method signatures (and defaults). Implemented with implements. Multiple interfaces allowed.

Casting

  • Upcast (sub -> super): automatic, safe
  • Downcast (super -> sub): explicit, risky; may throw ClassCastException

Problem-Solving Quick Reference

  • Check reference type for compile-time method access
  • Check object type for runtime method execution
  • super() must be first statement in subclass constructor
  • Abstract classes cannot be instantiated

🔬Ultimate Review Packet Materials

Download official review materials for this unit.

No URP materials available for this unit yet.

Check back soon for study guides, practice questions, and review videos.

← Back to AP Computer Science A