Skip to content

diegot-code/Python-Inheritance-Polymorphism

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Inheritance and Polymorphism Examples

This Python code demonstrates the concepts of Inheritance and Polymorphism in three scenarios: Vehicles, Animals, and Weapons.

Learning Objectives

  1. Inheritance:

    • Understand the fundamental principles of inheritance in object-oriented programming.
    • Learn how to create subclasses that inherit attributes and methods from a base class.
    • Explore the benefits of code reusability and hierarchy in class structures.
  2. Polymorphism:

    • Grasp the concept of polymorphism and its significance in object-oriented design.
    • Recognize how polymorphism enables treating different classes as instances of the same class.
    • Implement method overriding to customize behavior in derived classes.

Code Structure

1. Inheritance Example (Lines 3-47)

  • Classes:
    • Vehicle: Base class for different vehicles.
    • Subclasses: Boat, Plane, Car.
    • Instances: Illustrative instances.
    • Functionality: Demonstrates attribute and method inheritance.

2. Polymorphism Example (Lines 49-87)

  • Classes:
    • Animal: Base class with a generic eat method.
    • Subclasses: Cat, Dog, Horse, Mouse.
    • Instances: Objects of subclasses.
    • Functionality: Demonstrates polymorphism with overridden methods.

3. Polymorphism with Weapons Example (Lines 89-111)

  • Classes:
    • Weapon: Base class for weapons.
    • Subclasses: Sword, Bow, Dagger.
    • Instances: Instances of weapon subclasses.
    • Functionality: Utilizes polymorphism for different weapon attacks.

Usage

  • Uncomment specific lines to test different functionalities within each section.
  • Customize or extend the code to explore additional aspects of inheritance and polymorphism.

About

Intro to Inheritance and Polymorphism in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages