What is inheritance in object-oriented programming?
Inheritance defines a new class from an existing class, allowing it to receive accessible members, add members, or change inherited behavior.
Study 4 Inheritance with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is inheritance in object-oriented programming?
Inheritance defines a new class from an existing class, allowing it to receive accessible members, add members, or change inherited behavior.
What are the two main class roles in inheritance?
The base class is the existing superclass or parent; the derived class is the new subclass or child that inherits from it.
When is composition preferable to inheritance?
Inheritance should model an is-a relationship. Composition is usually better when one object merely contains or uses another, such as a Car having an Engine.
What does Bicycle inherit from Vehicle?
Bicycle is a Vehicle, so Bicycle can reuse Vehicle's inherited speed field and stop() method while adding gear and changeGear().
What does transitive inheritance mean?
Inheritance is transitive: if MountainBike derives from Bicycle and Bicycle derives from Vehicle, MountainBike also receives Vehicle's inherited behavior.
Are constructors generally inherited?
Constructors are generally not inherited. A derived constructor initializes its own state and may invoke a base-class constructor.
What does specialization mean in inheritance?
Specialization lets a derived class represent a more specific form of the base concept by adding behavior or details that distinguish it.
What is method overriding?
Overriding replaces an inherited instance method with a compatible implementation in the derived class, such as Dog replacing Animal.speak() with “Bark.”
How do overriding and overloading differ?
Overriding changes inherited behavior in a derived class; overloading defines methods with the same name but different parameter lists.
How can an override extend base behavior?
In Java, super can call the base implementation from an overriding method before the derived method adds its own behavior.
What is polymorphic use of a derived object?
Polymorphism allows a derived object to be used where its base type is expected, while the runtime object determines which override runs.
What determines an overridden method at runtime?
Runtime dispatch selects the method implementation according to the object's runtime type, so an Animal reference holding a Dog calls Dog's speak().