Ch 12 - Abstract Classes
A class provides default variables
Abstract class
A class that provides default code to other classes that use that class (Abstract class or interface)
Abstract class
T/F: Abstract classes provide compile-time polymorphism
False, Abstract classes provide runtime polymorphism, which enables a programer to use an abstract method without worrying about which concrete class implements the abstract method.
T/F: Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, and location. This program will benefit from an abstract class to represent the trees.
False, Because there is no information specific to each species of tree, each tree object can simply possess the species type, age, and location information
Consider a program that maintains a grocery list. Each item, like eggs, has an associated price and weight. Each item belongs to a category like produce, meat, or cereal, where each category has additional features, such as meat having a "sell by" date. This program will benefit from an abstract class.
False, Normal inheritance is sufficient. A superclass like Item might implement price and weight. Then subclasses like MeatItem might add behavior like a "sell by" date. But no behavior was mentioned that must be implemented by all subclasses.
T/F: An abstract class can be instantiated as an object
False, an abstract class is missing some behaviors. A subclass that implements those behavior can then be instantiated
A class that provides an API that must be implemented and no other code
Interface
A class that provides only static final fields
Interfaces, they can define public static final fields and don't restrict a class' inheritance
T/F: An abstract class cannot be instantiated, or created, because it has missing methods
True
T/F: UML uses italics to denote abstract classes
True
T/F: a class can implement multiple interfaces
True
T/F: an abstract class can include both method signatures for abstract methods and complete code for non-abstract methods
True
T/F: if a sublcass does not implement an abstract method, then the subclass must also be defined as abstract
True
T/F: Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, location, and estimated size based on age. Each species uses a different formula to estimate size based on age. This program will benefit from an abstract class.
True, a superclass tree might store the age and location, and specify an estimateSize() behavior. Subclasses for each species like OakTree then implements the species particular growth rate formula
abstract class
a class that guides the design of subclasses but cannot itself be instantiated as an object; specifies how the subclass must be implemented
concrete class
a class that is not abstract, and hence can be instantiated
abstract method
a method that each subclass must implement to be a concrete class
public class Circle extends Shape implements Serializable, DrawableInterface {
how to implement an interface
interface
specifies a set of methods that an implementing class must override and define
what word is needed to create an interface?
the keyword interface is needed in the class definition: public interface DrawableInterface {