Lecture on Inheritance

Jul 29, 2024

Lecture Notes on Inheritance

Introduction to Inheritance

  • Definition: Inheritance is a process of acquiring properties and behaviors from one class to another.

Advantages of Inheritance

  1. Code Reusability: Enables reuse of existing code.
  2. Reduced Time and Effort: Reduces time and effort by reusing code.
  3. Profitability: Increases company profitability through efficiency.

Understanding Properties and Behavior

  • Properties: Represented by data types (variables).
  • Behavior: Represented by methods/functions.
    • Examples:
      • Properties: height, color.
      • Behavior: readBooks().

UML Diagrams

  • Definition: A UML diagram is used to describe the structure of a system.
  • Components:
    • Class representation: A rectangle box with the class name inside.
    • Relationship indication: An arrow (extends) to indicate inheritance.

Types of Inheritance

  1. Single Level Inheritance: One parent class and one child class.
    • Example: Parent -> Child
  2. Multi Level Inheritance: Hierarchical relationship extending through multiple levels.
    • Example: Grandparent -> Parent -> Child
  3. Hierarchical Inheritance: One parent class gives rise to multiple child classes.
    • Example: Parent -> Child1, Child2, Child3
  4. Hybrid Inheritance: A combination of multiple types of inheritance, e.g., a multi-level inheritance combined with hierarchical.
    • Example: Combo of multiple types in one structure.
  5. Multiple Inheritance: One child class inherits from two parent classes. - Note: Not supported in Java due to the diamond problem.
    • Diamond Problem: Confusion arises if the child class inherits from two parent classes with similar attributes.
  6. Cyclic Inheritance: A class inherits from another class that indirectly inherits from itself, causing a cycle. - Note: Also not allowed in Java.

Important Concepts

  • Parent Class: The class from which properties and behaviors are inherited.
    • Also known as: Base Class or Superclass.
  • Child Class: The class that inherits from a parent class.
    • Also known as: Derived Class, Subclass.

Conclusion

  • Key Themes:
    • Inheritance is fundamental to Object-Oriented Programming (OOP).
    • Understanding these concepts is crucial for software design and implementation.
    • Important to master UML diagrams and recognize the types of inheritance for better software architecture.