The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ–²

Unit 9

9.1 Overview of Unit 9: Inheritance and 9.1 Creating Superclasses and Subclasses

3 min readβ€’november 16, 2020

Peter Cao


The Big Takeaway Of This Unit
Object-Orientated Programming Inheritance and polymorphism are two central pillars of object-orientated programming and refer to some of the ways abstraction is attained.

Unit Overview

Exam Weighting

  • 5-10% of the test
  • Roughly 2 to 4 multiple-choice questions
  • A possible topic of FRQ #4, which could test your ability to create a class, which could extend a parent class.

Enduring Understanding

After learning about classes with the first two principles of object-orientated programming and different data structures, it's time to learn about the last two principles of object-orientated programming: inheritance and polymorphism. Inheritance allows us to have classes that share the properties of another class. Polymorphism includes allowing an object to be called by both its class and the "parent class" as well.

Building Computational Thinking

In this unit, you'll learn how to use inheritance and polymorphism to make different classes. Using these, you'll reduce the code you would have to write otherwise with multiple unrelated classes. Once you learn about inheritance and polymorphism, you will know most of what you need to know for real-life object-orientated programming and can code most real-world situations!

Main Ideas for This Unit

  • Superclasses and Subclasses
  • Creating Subclasses
  • The super Keyword
  • Method Overriding
  • Dynamic and Static Typing
  • Polymorphism
  • The Object Superclass

9.1: Creating Superclasses and Subclasses

Introduction to Inheritance

In this unit, we will mainly focus on the last two principles of object-orientated programming. In the first half of the unit, we will discuss inheritance, while in the second half, we will discuss polymorphism. Inheritance is where one class, called the subclass, can share methods and instance variables with another class called the superclass.
When we make a subclass, the subclass can use all of the methods and instance variables of the superclass with the exact same implementation without having to write these again. However, we will learn of the exception to this in Topic 9.3. The subclass can also add its own methods and instance variables that are specific to the subclass.
When we use inheritance, we think of a subclass as a more specific type of the superclass. For example, let's create the superclass SchoolSubject, which contains objects like math, science, and PE, but we have a subclass called APSubject, which is a subclass of SchoolSubjects and contains objects like apCSA and apBio. Because of inheritance, all APSubject objects are also SchoolSubject objects, but this isn't true the other way around. We will discuss this more in Topic 9.5.
A subclass can only inherit from one superclass. This is because of the diamond problem in programming. Here is how it works:
  1. Consider a class A. A is the superclass to classes B and C.
  2. Classes B and C both have a new method named coolMethod(), each with its own implementation.
  3. Class D inherits both classes B and C.
  4. We make an object d of classD and call coolMethod(). Which version of coolMethod() will be called?
Because of this problem, the people who made Java decided that each class can inherit from a maximum of one superclass. However, one class can be the superclass for many subclasses.

Making Subclasses

Making subclasses in Java is very straightforward. To make a subclass, we just add the words extends SuperClassName to the class header of the subclass. Let's do an example. Suppose a class A is a superclass for class B, which itself is a superclass for class C, the superclass of classes D and E. Let's write the class headers of the five classes:
public class A { } public class B extends A { } public class C extends B { } public class D extends C { } public class E extends C { }

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ“±
Unit 2: Using Objects
πŸ–₯
Unit 3: Boolean Expressions and if Statements
πŸ•Ή
Unit 4: Iteration
βš™οΈ
Unit 5: Writing Classes
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.