ALAB 308A.2.2 - Static Methods, Static Properties, and Inheritance


Learning Objectives

After this lab, learners will have demonstrated the ability to:

  • Create static methods and properties in a class.
  • Call state methods and access static properties of a class without instantiating the class.
  • Create subclasses that inherit properties and methods from their parent class.
  • Add methods specific to child classes to extend their functionality.

 CodeSandbox

This lab uses CodeSandbox as one of its tools.

If you are unfamiliar with CodeSandbox, or need a refresher, please visit our reference page on CodeSandbox for instructions on:

  • Creating an Account
  • Making a Sandbox
  • Navigating your Sandbox
  • Submitting a link to your Sandbox to Canvas

Instructions

  1. Create a Vanilla CodeSandbox and name it "JavaScript Classes Lab 2."
  2. Follow along with the instructions below, adding to your index.js file.
  3. Submit the link to your CodeSandbox on Canvas when you are finished.

Deliverables

  • A link to a CodeSandbox that contains your completed classes with no errors (comment things out if they do not work).

Static Methods and Properties

class Governor {
    // something
}

There is only one Governor in the state; add static methods and properties you'd expect a Governor to have.

Log the properties and test the methods (but don't instantiate an object of the class).


Inheritance

Person{
    constructor () {
        // something
    }
}

For the Person class:

  • Think of three properties all people share, and set them with the constructor.
  • Think of three methods all people share, and create them.
  • Create a PostalWorker class that inherits from Person, and add some methods specific to postal workers.
  • Create a Chef class that inherits from Person, and add some methods specific to chefs.
  • Create two PostalWorkers and two Chefs. Log them and test all of their methods.

One Step Further

This section is optional, but complete it if you have the time.

Create the following classes using the given information.

Class Derived From Properties Methods
BankAccount n/a ownerName, balance, acctNum deposit, withdraw
CheckingAccount BankAccount overdraftEnabled Override withdraw to implement an overdraft feature.
SavingsAccount BankAccount None Override withdraw to disallow withdrawals completely.

acctNum should be generated within the constructor of BankAccount, not passed in as an argument.

Copyright © Per Scholas 2024