11. Classes

  • 11. Animal
  • Animal and CarnivoreAnimal.

  • 11. Candy
  • Food interface, Candy class, ComboCandy subclass.

  • 11. Inheritance 1
  • Animal. Dog. Cat. Borzoi. Boxer. Abstract example: A, B, C.

  • 11. Inheritance 2
  • Noisy, Colorable, Cat, RandomCat. Practice with classes, abstract classes, and interfaces.

  • Abstract Classes
  • When are abstract classes useful? A discussion of a few examples.

  • Interfaces
  • Concrete examples of interfaces.

  • 11. Quiz B
  • Clothes interface has int fashion() and int warmth(). HeadCovering is a class that implements the Clothes interface. Its constructor sets fashion and warmth, in that order. WoolClothes is a class that implements the Clothes interface. It takes in a Clothes object in its constructor. It adds 5 to the warmth provided by the Clothes because they are made of wool. Person is an abstract class with a String name field and a String getName() function, as well as abstract methods int getWarmth() and int getFashion().

  • 11. AQuiz IAnswers
  • A quiz asking you to write an interface, an abstract class, and more.

  • 11. Treasure Hunt
  • Create an interface Currency that has a double getGoldValue() method that returns the number of grams of gold that equal the worth of the item. Create a BankNote class that implements the Currency interface. A BankNote should be created in denominations of 1, 2, 5, 10, 20, 50, or 100, although you need not enforce this. A BankNote represents a single bill, so you might have lots of them. Add a toString method (see example code below).

  • 11. Instanceof
  • The instanceof operator tells you if a variable is an “instance of” (made from a subclass of) a given class. Example: if (x instanceof Cat) { ... }. Casting looks like this: (Cat) x. Casting can fail (with a “runtime exception”) if x is not really a Cat. This can happen inside functions that do not know where their inputs are coming from. Frequently you check to see if something is the right class before using a cast to “convert it” to that class.

Additional resources