Concepts / Method Overriding

Method Overriding

Inheritance is a mechanism that implements a type and subtype relationship between classes to enable code reuse.

  • Programming

The Shared-Behavior Problem

Related classes often have characteristics in common, but they may also need characteristics that apply only to one kind of object. Inheritance provides a way to organize that relationship: a parent class holds common characteristics, while child classes inherit those characteristics and add their own specific characteristics. Method overriding belongs to this broader design problem because it concerns how a subclass can provide behavior that is specific to that subclass while remaining part of the same class hierarchy.

Begin by asking which characteristics are common to every related class and which characteristics belong only to a particular subclass.

Parent and Subclass Types

Inheritance implements a type and subtype relationship between classes. The parent class represents the shared type, and each child class represents a more specific subtype. A subclass automatically possesses all attributes and methods from its parent class. It can also add characteristics that are specific to that subclass. This gives the hierarchy a general-to-specific structure rather than requiring every related class to repeat the same definitions.

generalizesgeneralizesParent classcommon characteristicsChild class Ainherited and specificcharacteristicsChild class Binherited and specificcharacteristics
How does a parent class relate to its subclasses, and how does shared structure flow from the parent into more specific classes?

The diagram shows the conceptual direction of the relationship. The parent is the place for characteristics shared by the related classes. Each child receives those inherited characteristics and may add characteristics that are not shared by the other children.

Shared and Specific Characteristics

Placing Characteristics in a Hierarchy

Imagine a generated hierarchy with a parent class called Vehicle and two child classes called Car and Bicycle. Decide where general and specific characteristics belong.

Find the common characteristics: Characteristics that apply to both Car and Bicycle belong conceptually in Vehicle. This follows the rule that a parent class holds common characteristics.

Place the inherited structure: Because Car and Bicycle are subclasses, they automatically possess the attributes and methods supplied by Vehicle.

Add subclass-specific characteristics: A characteristic that applies only to Car or only to Bicycle belongs in that specific child class rather than in the parent.

Consider specialized behavior: If a subclass needs behavior specific to itself, that behavior is part of the subclass-specific portion of the hierarchy. The supplied source explains the inheritance relationship but does not specify the detailed runtime rule for selecting between parent and subclass implementations.

The parent contains shared structure, while each subclass contains its own additional structure. This organization avoids placing unrelated characteristics in the parent and avoids repeating shared definitions in every child.

inherited byinherited byCommoncharacteristicsparent classCar-specificcharacteristicsCar subclassBicycle-specificcharacteristicsBicycle subclass
Which characteristics belong in the parent class, and which should remain specific to each subclass?

Overriding in Context

Method overriding should be understood as part of the parent-and-subclass relationship. The parent supplies common methods, and the subclass inherits them. A subclass may also need behavior that is specific to its own characteristics. That distinction is the conceptual reason overriding is associated with inheritance: shared behavior belongs to the parent, while specialized behavior belongs to the subclass.

specialized inassociated withParent methodshared behaviorSubclass methodspecific behaviorSubclass instancebelongs to the subtype
What changes when a subclass supplies behavior for a method that is also associated with the parent class?

Reuse Without Duplication

A subclass does not need separate definitions for every characteristic it shares with its parent. It automatically possesses the parent's attributes and methods. This is the code-reuse benefit of inheritance: common definitions can be kept in one parent class instead of being duplicated across related child classes.

Without a shared parentWith inheritance
Related classes repeat common attributes and methods.The parent stores common attributes and methods once.
A change to shared behavior may need to be repeated in multiple classes.Related classes inherit the common structure from the parent.
The relationship between the classes is less explicit.The parent and child classes express a type and subtype relationship.

Inheritance reduces duplication and makes maintaining related classes easier and more consistent.

Designing the Hierarchy

  1. List the characteristics shared by the related classes.
  2. Place those common characteristics in the parent class.
  3. Identify characteristics that apply only to one subclass.
  4. Place subclass-specific characteristics in the appropriate child class.
  5. Check that the hierarchy expresses a genuine general-to-specific relationship.
  6. Use inheritance to avoid repeating common attributes and methods.
  • Putting every characteristic into the parent class.

    The source distinguishes common characteristics from characteristics specific to child classes.

    Fix: Keep common characteristics in the parent and move subclass-specific characteristics into the relevant child.

  • Repeating inherited attributes and methods in every subclass.

    Subclasses automatically possess the attributes and methods of their parent.

    Fix: Place the shared definition in the parent so the subclasses can reuse it.

  • Treating inheritance only as a duplication shortcut.

    Inheritance implements a type and subtype relationship as well as enabling code reuse.

    Fix: Use inheritance when the class hierarchy represents a meaningful general-to-specific relationship.

Check Your Model

MEDIUM

You are designing a parent class named Account and two child classes named SavingsAccount and BusinessAccount. Decide which characteristics should be shared by Account and which should remain specific to one child. Then explain how placing the shared characteristics in Account reduces duplication.

Hints
  • Start by identifying characteristics that apply to both child classes.
  • Place those common characteristics in the parent.
  • Keep a characteristic in a child when it applies only to that child.
  • Use the type and subtype relationship to justify the hierarchy.

What do you think happens?

Before checking the explanation, predict what should happen to a characteristic shared by SavingsAccount and BusinessAccount when the hierarchy is redesigned to use Account as their parent.

  • Define it separately in both child classes
  • Place it in the parent class
  • Remove it because subclasses cannot share characteristics
Reveal answer

Answer: Place it in the parent class.

The source states that a parent class holds common characteristics and that subclasses inherit the parent's attributes and methods. This placement enables reuse and reduces duplication.

Key Takeaways

  1. Inheritance implements a type and subtype relationship between classes.
  2. A parent class holds common characteristics, while child classes add specific characteristics.
  3. Subclasses automatically possess the attributes and methods of their parent.
  4. Inheritance enables code reuse, reduces duplication, and supports easier, more consistent maintenance.
  5. Method overriding should be studied within this parent-and-subclass structure, while the exact runtime method-selection rule depends on details not provided in the supplied source.

Key Takeaways

  • Inheritance creates a general parent type and one or more more-specific child subtypes.
  • Common attributes and methods belong in the parent class.
  • Subclasses inherit the parent's attributes and methods and add their own specific characteristics.
  • This structure reduces duplication and makes related classes easier and more consistent to maintain.
  • The supplied material establishes the inheritance model but does not provide the language-specific execution rule for method overriding.