PSOOP Lab 07 Abstract Classes 2024 25
PSOOP Lab 07 Abstract Classes 2024 25
• Runtime Polymorphism
• Abstraction
• Abstract class
2
Recap – Dynamic Method Dispatch
• A superclass reference variable can refer to a subclass object. Java uses this fact to
resolve calls to overridden methods at run time.
• When an overridden method is called through a superclass reference, Java determines
which version of that method to execute based upon the type of the object being referred
to at the time the call occurs.
• Thus, it is the type of the object being referred to (not the type of the reference variable)
that determines which version of an overridden method will be executed.
16
Abstract Classes Safety
1. A class with one or more abstract methods must be declared
abstract.
- Syntax error if not done.
- Can still decide to make class abstract even if no abstract
methods.
2. Objects of an abstract type cannot be instantiated.
-- Can still declare variables of this type
3. A subclass must implement all inherited abstract methods or
be abstract itself.
17
Sub Classes of Abstract Classes
• Classes that extend an abstract class must
provide a working version of all abstract
methods from the parent class
• or they must be declared to be abstract as
well
• could still decide to keep a class abstract
regardless of status of abstract methods
18
Abstract Class Summary
• A class that is declared with abstract keyword