wk 10: inheritance, polymorphism, and interfaces

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/17

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:49 AM on 4/28/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

18 Terms

1
New cards

inheritance

lets one class (the subclass) reuse and extend another class (the superclass).

2
New cards

private

only the class can use that variable directly

3
New cards

public

anyone can use the variable directly

4
New cards

protected

it’s the same group of programs but not everyone can see

5
New cards

inheritance code example base class

/ Base class (superclass)

public class Vehicle {

protected String brand;

protected int year;

public Vehicle(String brand, int year) {

this.brand = brand;

this.year = year;

}

public void startEngine() {

System.out.println("The vehicle's engine starts...");

}

}

6
New cards

inheritance code example subclass

// Derived class (subclass)

public class Car extends Vehicle {

private int doors;

public Car(String brand, int year, int doors) {

super(brand, year); // Call superclass constructor

this.doors = doors;

}

@Override

public void startEngine() {

System.out.println("The car engine roars to life!");

}

7
New cards

polymorphism

means many forms

when we use a superclass reference to refer

to a subclass object, we can call the methods defined in the superclass — and if they’re

overridden, the subclass’s version runs instead

8
New cards

polymorphism example code

Vehicle myVehicle = new Car("Honda", 2022, 4);

myVehicle.startEngine(); // The car engine roars to life!

Vehicle[] garage = {

new Car("Toyota", 2021, 4),

new Truck("Ford", 2018, 2),

new Motorcycle("Yamaha", 2022)

};

for (Vehicle v : garage) {

v.startEngine(); // Calls each subclass’s version

}

9
New cards

design considerations

  1. Favor composition over inheritance — Sometimes a class should use another rather than

extend it.

2. Use inheritance for “is-a” relationships — A Car is a Vehicle, but a Garage is not a Vehicle.

3. Mark overridden methods with @Override — This catches mistakes at compile time.

4. Keep superclass fields protected or private — Maintain encapsulation and provide access

through methods.

5. Interfaces define roles — A class can “play” multiple roles by implementing multiple

interfaces.

6. Abstract classes define common state and partial behavior — Use them when you need

both shared code and required methods.

10
New cards

abstract class definition

partial blueprint that defines what an objec tis

you want to a common base with some shared code and some forced implementation

11
New cards

when can you use abstract class

classes share a common root for exampel dog and cat are both animals

12
New cards

how may abstract classes can be inherited by a class

one

13
New cards

interface definition

a contract/plugin; defines what an object can do; can only have constant variables

you only care about what an object can do

is only static final and method signatures

14
New cards

when should you use an interface

when you want to guarantee that a class has a certain skill regardless of what it is

15
New cards

how many interfaces can a class implement

as many as it wants

16
New cards

overloading

same method name, different parameters

17
New cards

overriding

child class changes parent method behavior

18
New cards

private vs protected

protected can be accessed by same subclasses