Home Lesson 9 Hierarchy
Post
Cancel

Lesson 9 Hierarchy

9.1 Superclasses and Subclasses

A hierarchy of classes is created, extending attributes into subclasses of information (ie. Automobile –> Trucks and Sedans –> Ford, BMW, Nissan, Toyota).

Pre-Requisites

Before we start you need to remember what classes and methods are. Scopes of variables are also important to this section because you need to know which classes can access which variables before extending a variable across classes.

Scope of Variables:

Variables can be declared as private or public.

Popcorn Hack: What are scope do private and public variables allow?

PrivatePublic
only directly accessible within the class they are declaredaccessible anywhere inside and outside their class

These are different types of variables. Only INSTANCE and STATIC variables can be declared as private or public.

Variable TypeDefinitionScope within ClassScope to Subclasses
Local Variablesvariables within methodscannot be accessed outside methodcannot be accessed outside class
Instance Variablesvariables within a class but not inside a methodcan be accessed in entire classcan be accessed in subclasses
Static Variablevariables that belong to a class, not instancecan be accessed in entire classcan be accessed in subclasses
Parameter Variablevariables specific in passing values to the methodcannot be accessed outside methoddo not affect inheritance

Now we can go into class hierarchies.

Class Hierarchy

Definitions

  • Superclasses - a class that contains all the common attributes and behaviors that could be shared among other classes (a blueprint for subclasses)
  • Subclasses - extends the behaviors to is specified by a superclass; can also have additional specific attributes
  • “Is-A” Relationship - the relationship when a subclass extends a superclass (ie. Automobile –> Sedan; a Sedan “is-a” automobile)

extends Keyword

extends the subclass from the superclass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Automobile {
  public String brand; // public instance var
  private String model; // private instance var

  public Automobile(String brand, String model) {
    this.brand = brand;
    this.model = model;
  }

  public void start() {
    System.out.println("Car is starting");
  }
}

class Truck extends Automobile {
  public int cargoCapacity; // subclass specific var

  // instance that is specific to the Truck subclass, with vars from Automobile class
  public Truck(String brand, String model, int cargoCapacity) {
    super(brand, model); // inherited vars
    this.cargoCapacity = cargoCapacity;
  }

  // specific method to Truck
  public void loadCargo() {
    System.out.println("Loading cargo into the truck");
  }
}

class Sedan extends Automobile {
  public boolean isLuxury; // subclass specific var

  // instance that is specific to the Sedan subclass, with vars from Automobile class
  public Sedan(String brand, String model, boolean isLuxury) {
    super(brand, model); // inherited vars
    this.isLuxury = isLuxury;
  }

  // specific method to Sedan
  public void accelerate() {
    System.out.println("Sedan is accelerating");
  }
}

public class Main {
    public static void main(String[] args) {
        Automobile car = new Automobile("Toyota", "Camry");
        Truck truck = new Truck("Ford", "F-150", 1000);
        Sedan sedan = new Sedan("BMW", "328i", true);

        // automobile methods and variables
        System.out.println(car.brand); // Accessing public variable
        // System.out.println(car.model); // compilation error because of the private var
        car.start();


        // truck methods and variables
        System.out.println(truck.brand); // inherited public var
        System.out.println(truck.cargoCapacity); // public var specific to truck
        truck.loadCargo();


        // sedan methods and variables
        System.out.println(sedan.brand); // inherited public var
        System.out.println(sedan.isLuxury); // public var specific to sedan
        sedan.accelerate();
    }
}

Main.main(null);
1
2
3
4
5
6
7
8
Toyota
Car is starting
Ford
1000
Loading cargo into the truck
BMW
true
Sedan is accelerating

This example shows how the Automobile class is extended twice, with the Truck and Sedan subclasses.

Popcorn Hack: If I were to declare a variable color that is private in the class Automobile, would I be able to extend and directly access that variable to the subclass Truck or Sedan?

A: No, you would need to encapsulate the private variable as shown above, through a method in the superclass and then inherit that var with super(), which will be explained later.

Hacks

Create a superclass with at least 2 subclasses based on your own topic.

  • Create a DrawIO diagram for your structure
  • Create a superclass on your own topic
  • Create at least two subclasses
  • Each class must create at least two methods, one private and public variable, and examples of local, static, instance, and parameter variables
This post is licensed under CC BY 4.0 by the author.

Plan 11

Lesson 9.1 Hierarchy Student