Fiveable
Fiveable

Superclass

Definition

A superclass, also known as a parent class or base class, is a class that is extended by another class (subclass). It provides common attributes and behaviors that can be inherited by its subclasses.

Analogy

Think of a superclass as a template for creating different types of vehicles. For example, the superclass "Vehicle" could have common features like wheels, engine, and steering. Now, when you create specific types of vehicles like "Car" or "Motorcycle," they inherit these common features from the superclass while adding their own unique characteristics.

Related terms

Subclass: A subclass is a class that extends another class (superclass). It inherits all the attributes and behaviors from its superclass while adding its own additional features.

Inheritance: Inheritance is the mechanism in Java where one class acquires properties (fields and methods) from another class. It allows code reuse and promotes hierarchical organization of classes.

Method Overriding: Method overriding occurs when a subclass provides its own implementation of a method that already exists in its superclass. The overridden method in the subclass replaces the original implementation provided by the superclass.

"Superclass" appears in:

Practice Questions (19)

  • If the class Bird is a superclass of the class Penguin, what happens when Java tries to run the line “Penguin penguin = new Bird();”?
  • 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?
  • If Class J is a superclass of Class K and Class L, which of the following are valid?
  • If class C is class B's superclass and class A is class C's superclass, what are the appropriate class headers for each of the three classes?
  • In Java, can a subclass object be assigned to a superclass reference variable?
  • In Java, what class is the superclass of all other classes?
  • 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?
  • Consider the following superclass and subclass declarations: ```java public class Shape { private String color; public Shape(String color) { this.color = color; } public String getColor() { return color; } } public class Circle extends Shape { private double radius; public Circle(String color, double radius) { // Circle constructor implementation goes here } } ```
  • 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:
  • 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 access modifier will the public methods inherited from a superclass have in the subclass?
  • When writing constructors for subclasses, which keyword is used to invoke the superclass's constructor?
  • 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.