Fiveable
Fiveable

Subclass

Definition

A subclass is a class that inherits properties and behaviors from another class, called the superclass. It can add new features or modify existing ones.

Analogy

Think of a subclass as a specialized version of a superclass. Just like how a sports car is a specialized version of a regular car, with added speed and performance features, a subclass adds specific functionalities to the superclass.

Related terms

Superclass: The superclass is the class from which the subclass inherits properties and behaviors.

Inherit: Inheritance is the process by which one class acquires properties and behaviors from another class.

Polymorphism: Polymorphism allows objects of different classes to be treated as objects of the same superclass, enabling flexibility in programming.

"Subclass" appears in:

Practice Questions (19)

  • If the class Car is a subclass of the class Vehicle, what happens when Java tries to run the line “Vehicle vehicle = new Car();”?
  • If Class C is a superclass of Class B and a subclass of Class A, which of the following is NOT valid?
  • Suppose class A is the superclass of class B and class C, and it is also the subclass of class D. If we have a method that takes in an object of class A, which of the following objects would we also be able to pass in to the method?
  • In Java, can a subclass object be assigned to a superclass reference variable?
  • If we don’t want our subclass method to do anything different from the superclass's implementation of that method, what should we do?
  • Which of the following correctly calls a superclass method in a subclass?
  • If a method of a superclass is overridden in the subclass, which method is called by the line "methodName();" in the subclass?
  • If you try to assign an object of a subclass to a reference variable of the superclass type, what will happen?
  • Which of the following are inherited by a subclass?
  • If a superclass's constructor takes in four parameters, how many parameters should the subclass's constructor have?
  • If a subclass's constructor doesn't explicitly call its superclass's constructor using super, Java will:
  • Which of the following is true regarding the return type of an overriding method in a subclass?
  • When should we write Javadoc comments for an overridden method in the subclass?
  • When a subclass defines a method with the same name, return type, and parameters as a method in its superclass, it is an example of:
  • What can a subclass introduce that is not defined in the superclass?
  • If a subclass does not provide an overridden implementation of an inherited method, what will happen?
  • Which keyword is used in the class header to create a subclass in Java?
  • How many subclasses can a superclass have in Java?
  • Can a subclass have its own variables and methods in addition to those inherited from the superclass?


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.