Animal and CarnivoreAnimal.
Food interface, Candy class, ComboCandy subclass.
Animal. Dog. Cat. Borzoi. Boxer. Abstract example: A, B, C.
Noisy, Colorable, Cat, RandomCat. Practice with classes, abstract classes, and interfaces.
When are abstract classes useful? A discussion of a few examples.
Concrete examples of interfaces.
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().
A quiz asking you to write an interface, an abstract class, and more.
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).
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.