1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
inheritance
lets one class (the subclass) reuse and extend another class (the superclass).
private
only the class can use that variable directly
public
anyone can use the variable directly
protected
it’s the same group of programs but not everyone can see
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...");
}
}
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!");
}
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
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
}
design considerations
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.
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
when can you use abstract class
classes share a common root for exampel dog and cat are both animals
how may abstract classes can be inherited by a class
one
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
when should you use an interface
when you want to guarantee that a class has a certain skill regardless of what it is
how many interfaces can a class implement
as many as it wants
overloading
same method name, different parameters
overriding
child class changes parent method behavior
private vs protected
protected can be accessed by same subclasses