Back to Inheritance (NOT REQUIRED)

Lesson PreviewLesson 2 of 7 in this unit

9.2 EXTRA: Writing Constructors for Subclasses

Part of Inheritance (NOT REQUIRED) in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

In Java, constructors are not inherited. This means that if you create a subclass, it does not automatically have the constructors of its superclass. Instead, each class must define its own constructor. If you do not explicitly create a constructor for your subclass, Java creates a default no-argument constructor that calls the superclass’s no-argument constructor for you.

You can use the super keyword to invoke the superclass's constructor. When you use super() without any parameters, it calls the no-argument constructor of the superclass. This allows programmers to simplify unnecessary, excessive code. Super() can only be invoked on the first line of the subclass constructor, otherwise it will cause a compilation error.

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.