zuai-logo

Inheritance

Sophie Anderson

Sophie Anderson

9 min read

Listen to this study note

Study Guide Overview

This unit covers object-oriented programming concepts of inheritance and polymorphism. It explores superclasses and subclasses, creating subclasses using the extends keyword, and the super keyword for constructors and methods. Method overriding, dynamic and static typing, and the Object superclass are also discussed. Finally, the unit introduces inheritance hierarchies and the concept of polymorphism.

The Big Takeaway of this Unit

** Object-Oriented Programming: Inheritance and polymorphism are two central pillars of object-orientated programming and refer to some of the ways abstraction is attained.**

Exam Weighting

  • 5-10% of the test
  • Roughly 2-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

superclass is a general class that serves as a parent or base class for other classes. Subclasses are classes that are derived from a superclass, and they inherit the methods and attributes of their parent class. Inheritance is the ability of a subclass to inherit or acquire the properties of its parent class. It allows subclasses to specialize the behavior of a superclass without having to rewrite the code. This helps reduce code duplication and makes code maintainable.

Inheritance can be used to model real-world relationships between classes. For example, suppose you have an Animal class and a Dog class. The Dog class could inherit from the Animal class and acquire all the properties of the Animal class, such as the ability to move and eat. Moving and eating are properties all animals have, so Animal is the superclass. A Dog class could have methods like bark and wagTail, but not all animals can do that, so the Dog class is the subclass in this relationship.

To create a subclass in Java, you have to use the extends keyword in the header of the subclass.

public class Dog extends An...

Question 1 of 10

In the context of inheritance, if 'Animal' is a general class, and 'Dog' and 'Cat' are more specific classes, which of the following is true? 🐕

Animal is the subclass, and Dog and Cat are the superclasses

Animal is the superclass, and Dog and Cat are subclasses

All three are superclasses

All three are subclasses