Back to Inheritance (NOT REQUIRED)

Lesson PreviewLesson 1 of 7 in this unit

9.1 EXTRA: Creating Superclasses and 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.

When you create a class, you are making a blueprint that tells the computer what properties (fields) an object will have and what actions (methods) it can perform. For example, consider a simple class called Vehicle. In this class you can include common properties like the color and the year, and a method that tells you when the vehicle starts. Look at this code:

java
public class Vehicle {
  public String color; // Vehicle color
  public int year; // Production year
  
  // Constructor: special method to initialize a new Vehicle
  public Vehicle(String color, int year) {
    this.color = color; // 'this.color' means the field in this object.
    this.year = year;
  }

  // A method that simulates starting the vehicle.
  public void start() {
    System.out.println("The vehicle is starting.");
  }
}

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.