What are a superclass and a subclass?
The superclass is the existing parent or base class; the subclass is the more specialized child or derived class.
Study 10 Inheritance and Polymorphism with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What are a superclass and a subclass?
The superclass is the existing parent or base class; the subclass is the more specialized child or derived class.
What does `extends` declare in Java?
The `extends` keyword declares that one Java class inherits from another class.
How does a subclass call a superclass constructor?
A subclass constructor can call a superclass constructor with `super(...)`, initializing state defined by the superclass.
What is method overriding?
Method overriding occurs when a subclass supplies a new implementation of an inherited instance method with the same name and compatible parameters.
How do overloading and overriding differ?
Overloading defines methods with the same name but different parameter lists; overriding replaces inherited behavior in a subclass.
What does polymorphism allow?
Polymorphism lets a superclass reference or parameter refer to objects of multiple compatible subclass types.
What is dynamic method dispatch?
Dynamic method dispatch selects an overridden instance method according to the object's runtime type, not merely its declared reference type.
What is upcasting?
Upcasting assigns a subclass object to a superclass reference; it is safe and automatic because the subclass satisfies the superclass type.
When can a downcast fail?
A downcast is valid only when the object is actually an instance of the target subtype; otherwise Java can throw `ClassCastException`.
What is an abstract class?
An abstract class cannot be instantiated directly and may declare abstract methods that subclasses must implement.
Are static methods overridden?
Static methods are hidden rather than overridden in the runtime-dispatch sense used for instance methods.
When is composition preferable to inheritance?
Composition models a has-a relationship: one object contains or delegates work to another, such as a `Car` containing an `Engine`.