Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ducks — Learn OOP with P.I.E.

Object Oriented Programming is easy as P.I.E. (Polymorphism, Inhereitence, and Encapsulation)

Summary

This small sample demonstrates three core OOP concepts using a simple Duck hierarchy:

  • Polymorphism — different duck types override common behaviors.
  • Inhereitence — concrete ducks inherit state/behavior from the base Duck.
  • Encapsulation — fields are backed by properties with validation and controlled access.

What you'll learn

  • How a base class defines common behavior and state.
  • How derived classes override behavior (polymorphism).
  • How to hide internal state and expose controlled accessors (encapsulation).

Files to inspect

Quick run

From the repository root run:

cd Ducks
dotnet build Ducks.csproj
dotnet run --project Ducks.csproj

The program prints short messages demonstrating each concept. Look at how MakeSound() and Fly() are called on the base Duck reference but produce different results per concrete duck — that is polymorphism. The Rubber class shows encapsulation by controlling its Name property (defaulting invalid names). The Mallard and Wooden classes illustrate inheritance from the common Duck base.

Teaching notes (quick pointers)

  • Polymorphism: search for public virtual void MakeSound() and the override implementations in derived classes.
  • Inhereitence: note how Mallard, Wooden, and Rubber simply declare : Duck and reuse base behavior.
  • Encapsulation: inspect private backing fields (e.g., _name) and the corresponding public property Name in Rubber.

Attribution

Author: Joe Valentine Original: 2017 Updated: Feb 22, 2026

Support

If this project helped you, please consider buying me a coffee:

https://buymeacoffee.com/jwvalentine

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages