1 / 45

Inheritance Version 1.0

Inheritance Version 1.0. Topics. Inheritance Constructors and Inheritance Hiding Methods and Variables Designing with Inheritance. Objectives. After completing this topic, students should be able to: Correctly design and use classes that use inheritance in a C# program

lars
Télécharger la présentation

Inheritance Version 1.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. InheritanceVersion 1.0

  2. Topics Inheritance Constructors and Inheritance Hiding Methods and Variables Designing with Inheritance

  3. Objectives After completing this topic, students should be able to: Correctly design and use classes that use inheritance in a C# program * Know how to correctly call the parent constructor * know how to hide methods and data in the parent class * Know when to use the protected attribute

  4. Inheritance is the act of deriving a new class from an already existing class. It is analogous to creating a new house blueprint from an existing one. Inheritance is useful when we find a natural hierarchical relationship between classes. Inheritance makes it possible to re-use existing code, thus saving time and minimizing bugs.

  5. Example and Terminology

  6. Suppose that we are creating a new role playing game, and we want to have the following creatures: dwarves elves fairies

  7. All of these are “creatures “. They all have • A name • A strength value • A hitpoint value dwarves elves fairies

  8. We could define a class for each such as: • public class Dwarf • { • private string name; • private intstrength; • private inthitpoints; • //other dwarf data • publicDwarf( ); • public GetDamage( ); • //other dwarf methods • } • public class Elf • { • private string name; • private intstrength; • private inthitpoints; • //other elf data • publicElf( ); • public GetDamage( ); • //other elf methods • } • public class Fairy • { • private string name; • private intstrength; • private inthitpoints; • //other fairy data • publicFairy( ); • public GetDamage( ); • //other fairy methods • }

  9. Note that all of these classes have some things in common • public class Dwarf • { • private string name; • private intstrength; • private inthitpoints; • //other dwarf data • publicDwarf( ); • public GetDamage( ); • //other dwarf methods • } • public class Elf • { • private string name; • private intstrength; • private inthitpoints; • //other elf data • publicElf( ); • public GetDamage( ); • //other elf methods • } • public class Fairy • { • private string name; • private intstrength; • private inthitpoints; • //other fairy data • publicFairy( ); • public GetDamage( ); • //other fairy methods • }

  10. And They Have Some Things That Are Different • public class Dwarf • { • private string name; • private intstrength; • private inthitpoints; • //other dwarf data • publicDwarf( ); • public GetDamage( ); • //other dwarf methods • } • public class Elf • { • private string name; • private intstrength; • private inthitpoints; • //other elf data • publicElf( ); • public GetDamage( ); • //other elf methods • } • public class Fairy • { • private string name; • private intstrength; • private inthitpoints; • //other fairy data • publicFairy( ); • public GetDamage( ); • //other fairy methods • }

  11. We gain a lot of productivity and functionality if we factor the common elements out into a base class. Creature name strength hitpoints Base class • Sometimes also called • parent class • super class This relationship is called Inheritance. Derived Class • Sometimes also called • child class • sub class Dwarf Elf Fairy

  12. If elves and fairies are both magical creatures, we might envision another layer of base and derived class Creature Base class name strength hitpoints Magical Creature Derived Class spells Dwarf Derived Class Elf Fairy

  13. This is known as a class hierarchy Creature name strength hitpoints Dwarf inherits from Creature Magical Creature inherits from Creature Magical Creature spells Dwarf Elf and Fairy inherit from Magical Creature Elf Fairy

  14. This is known as a class hierarchy Creature name strength hitpoints Common things are defined in base classes Magical Creature spells Dwarf Unique things are defined in derived classes Elf Fairy

  15. Let’s look at the base class definition Creature name strength hitpoints public class Creature { protectedstring name; protectedintstrength; protectedinthitpoints; public Creature( ) { …}; public intgetDamage( ) {…}; . . . } the protectedmodifier (#) tells us that the variable is accessible from within the Creature class and from within any derived classes. That is, methods of the derived class can see the protected data members defined in the base class.

  16. And the MagicalCreature class Creature name strength hitpoints public class MagicalCreature: Creature { protected intspells; public MagicalCreature( ) {…}; . . . } Magical Creature spells The : means that this class inherits from the Creature class. That is, a MagicalCreature object will have everything that a Creature object has plus anything uniquely defined in The Magical Creature class.

  17. So . . . If I create a MagicalCreature • object named Zor, then Zor has the • following properties: • A name • strength • hitpoints • spells - This comes from the MagicalCreature class These come from the Creature class

  18. When data is declared as protected in a parent class, methods in a child class can see this data Protected Creature data MagicalSpell method

  19. But be careful … methods in the parent class cannot see any data fields in the child part of the object. Creature method MagicalCreature data

  20. This kind of inheritance is known as an is-a relationship. That is, a MagicalCreatureis a Creature. An object of the MagicalCreature class can be used anyplace that an object of the Creature class can be used.

  21. Let’s look at how we deal with differences between classes by Looking at the GetDamage( ) method. GetDamage( ) computes and returns the damage that this creature inflicts in one round of combat. Every creature inflicts damage that is a random number between 1 and the creature’s strength value. Dwarf Elf Fairy

  22. Magical creatures can double the damage points if they have a magic spell. Dwarves are good fighters, and have a 25% chance of inflicting an additional 50 damage points Fairies are very quick, so they can attack twice.

  23. Since the damage that any creature can inflict is defined as a random number between 0 and the creature’s strength value, we could write the following code in the base Creature class: Creature name strength hitpoints public intGetDamage( ) { Random rgen = new Random( ); intdamage = rgen.Next(0, strength + 1); return damage; }

  24. Creature But if we create a Dwarf object “dw1”, and invoke the GetDamage method, this method will be executed, because a Dwarf is-a Creature. name strength hitpoints public intGetDamage( ) { Random rgen = new Random( ); intdamage = rgen.Next(0, strength + 1); return damage; } Dwarf

  25. But …Dwarves have a 25% chance of inflicting an additional 50 damage points. So the GetDamage( ) method in the Dwarf class should look something like this: Creature name strength hitpoints public intGetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); if (rgen.Next(1, 101) < 25) damage = damage + 50; return damage; } Dwarf

  26. Now both the Creature class and the Dwarf class have a method named GetDamage( ). If we have a Dwarf object “dw1”, how do we get the compiler to use the GetDamage method in the Dwarf class? public intGetDamage( ) { Random rgen = new Random( ); intdamage = rgen.Next(0, strength + 1); return damage; } Creature name strength hitpoints public intGetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); if (rgen.Next(1, 101) < 25) damage = damage + 50; return damage; } Dwarf

  27. Hiding a Method in the Base Class We hide, a method in the base class by writing a similar method in the derived class that has exactly the same signature, but with a different implementation. Then use the keyword new in the derived class method to tell the compiler to use this method on a derived object instead of the method of the same name in the base class.

  28. To Tell C# That We Want to hide the GetDamage Method in the base class public intGetDamage( ) { Random rgen = new Random( ); intdamage = rgen.Next(0, strength + 1); return damage; } Creature name strength hitpoints tell the compiler that this method over-rides the GetDamage method in the base class by using the keyword new. public newintGetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); if (rgen.Next(1, 101) < 25) damage = damage + 50; return damage; } Dwarf

  29. So, if I create a Dwarf object named dw1, and send dw1 a GetDamage( ) message . . . dw1.GetDamage( ) dw1 The GetDamage( ) method in the Dwarf class hides the GetDamage( ) method in the base class, and so the GetDamage( ) method in the Dwarf class gets executed.

  30. Let’s let dwarves have a unique property of size. The class definition then might be: dwarf public class Dwarf : Creature { private intsize; public Dwarf( ); public Dwarf(string, int, int, int); public intGetDamage( ); . . . }

  31. Creature Part Now create a Dwarf object … Dwarf dw1 = new Dwarf( “Rohan”, 350, 500, 25); When the constructor is called, the computer looks at the Dwarf class to see how much storage to allocate. It notes that a Dwarf is a Creature, so it looks at the Creature class also. Enough storage is allocated for the data members of both the base and the derived classes. name strength hitPoints Dwarf Part size dw1

  32. The Dwarf Constructor Dwarf::Dwarf (string _nme, int _strngth, int _hpts, int _sze) : base (_nme, _strngth, _hpts) { size = _sze; } Constructors are not inherited. In order to initialize the parent class we must invoke the base class constructor. If you do not explicitly call the Creature constructor, the default Creature constructor is called automatically. The base class constructor finishes executing before the derived class constructor does.

  33. Hiding Variables If a derived class declares a variable using the same name as a variable in a parent class, the variable in the child class is said to hide the variable in the parent. You have to use the new keyword in the derived class.

  34. Inheritance and References Because of the is-a relationship, an object of a derived class can always be treated as an object of the corresponding base class. In particular, you can always store the reference to a derived class object in a base class reference (upcast).

  35. Dwarf littleDwarf= new Dwarf(“Egrew”, 600, 500, 2); Creature littleCreature; littleCreature= littleDwarf; littleDwarf is A Dwarf object. littleCreature littleCreatureis a Creaturereference littleDwarf

  36. Because littleCreature is a Creature reference you cannot directly access Dwarf data. Dwarf littleDwarf= new Dwarf(“Egrew”, 600, 500, 2); Creature littleCreature; littleCreature= littleDwarf; Console.WriteLine(littleCreature.Size); littleDwarf is A Dwarf object. littleCreature littleCreatureis a Creaturereference littleDwarf

  37. You can store the reference to a base class object in a derived class reference, but you must do an explicit cast to make this work. Dwarf littleDwarf; Creature anyOne = new Creature(“joe”, 400, 190); littleDwarf = (Dwarf)anyOne;

  38. This is pretty dangerous and not often used, because the derived class reference thinks it is referencing a derived class object, but it really isn’t. This is referred to as the “slicing problem”. base part littleDwarf there is no derived part. If you try to access member data in the derived part, you will get garbage! joe

  39. Upcasting vs. Downcasting Casting from a descendant type to an ancestor type is known as upcasting. It is always safe, since you are moving up the inheritance hierarchy. In our case, for example, we are always know that a dwarf is an creature. Casting from an ancestor type to a descendant type is called downcasting. In our case, we can’t guarantee that every creature is a dwarf, so downcasting a creature to a dwarf can be very dangerous, since we assume that information may be there that isn’t.

  40. Designing With Inheritance

  41. Designing With Inheritance Usually we start with a notion of some very specific classes (Fairy, Elf, Dwarf … ) and move to more general classes by factoring out common data and operations.

  42. Designing With Inheritance For example, suppose that we wanted to write a program that deals with instruments in an orchestra Violin Oboe Viola Clarinet Kettle Drum . . .

  43. Designing With Inheritance Now design a class that contains all of the things that orchestra instruments have in common. * Instrument name * Owner * Where it is in the orchestra Then write methods to manage this data

  44. Designing With Inheritance Look at all of the instruments in the orchestra. can they be organized into some different groups where each group has some things in common. Instrument Woodwinds Brass Percussion String

  45. Instrument Woodwinds Brass Percussion String Clarinet French Horn Kettle Drum Violin Trumpet Cymbals Oboe Cello

More Related