Interfaces and Abstract Classes Flashcards
7 cards from real AP CSA practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Interfaces and Abstract Classes flashcards as text
Which keyword is used to declare an abstract class in Java?
Answer: abstract
The `abstract` keyword is placed before the `class` keyword to declare a class as abstract, meaning it cannot be instantiated directly.
Can an abstract class contain non-abstract (concrete) methods?
Answer: Yes, it can contain both abstract and concrete methods
An abstract class can have a mix of abstract methods (no body) and concrete methods (with a body), providing partial implementation.
What is the result of trying to instantiate an abstract class directly?
Answer: A compile-time error
Attempting to instantiate an abstract class results in a compile-time error because abstract classes are incomplete by design.
Which relationship does abstract class inheritance use in Java?
Answer: extends
A subclass inherits from an abstract class using the `extends` keyword, the same as with concrete class inheritance.
Consider: `public abstract class Shape { public abstract double area(); }`. Which of the following correctly creates a subclass?
Answer: public class Circle extends Shape { public double area() { return 3.14 * r * r; } }
Circle must extend Shape (not implement) and must provide a concrete `area()` method to avoid being abstract itself.
What distinguishes an abstract class from an interface in Java (in the context of AP CSA)?
Answer: Abstract classes can have constructors; interfaces cannot
Abstract classes can have constructors that subclasses can call via `super()`, whereas interfaces cannot have constructors because they cannot be instantiated.
A class Truck extends an abstract class Vehicle that has abstract methods `start()` and `stop()`. Truck provides `start()` but NOT `stop()`. What must be true of Truck?
Answer: Truck must also be declared abstract
Since Truck does not implement all abstract methods inherited from Vehicle, Truck itself must be declared abstract.