Chapter 6. Inheritance Your Object’s Family Tree
Sometimes you DO want to be just like your parents.
Ever run across a class that almost does exactly what you want your class to do? Found yourself thinking that if you could just change a few things, that class would be perfect? With inheritance, you can extend an existing class so your new class gets all of its behavior—with the flexibility to make changes to that behavior so you can tailor it however you want. Inheritance is one of the most powerful concepts and techniques in the C# language: with it, you can avoid duplicate code, model the real world more closely, and end up with apps that are easier to maintain and less prone to bugs.
Calculate damage for MORE weapons
Most of the code in ArrowDamage will be identical to the code in the SwordDamage class. Here’s what we need to do to get started building the new app.
Create a new .NET Console App project. We want it to do both sword and arrow calculations, so add the SwordDamage class to the project. The constants aren’t used anywhere else in the app, so make them private.
Create an ArrowDamage class that’s an exact copy of SwordDamage. Create a new class called ArrowDamage, ...
Get Head First C#, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.