Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Version 1.0. Topics Inheritance Constructors and Inheritance Hiding Methods and Variables Designing with Inheritance.

Similar presentations


Presentation on theme: "Inheritance Version 1.0. Topics Inheritance Constructors and Inheritance Hiding Methods and Variables Designing with Inheritance."— Presentation transcript:

1 Inheritance Version 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: dwarveselvesfairies

7 All of these are “creatures “. They all have A name A strength value A hitpoint value dwarveselvesfairies

8 We could define a class for each such as: public class Dwarf { private string name; private int strength; private int hitpoints; //other dwarf data public Dwarf( ); public GetDamage( ); // other dwarf methods } public class Elf { private string name; private int strength; private int hitpoints; //other elf data public Elf( ); public GetDamage( ); // other elf methods } public class Fairy { private string name; private int strength; private int hitpoints; //other fairy data public Fairy( ); public GetDamage( ); // other fairy methods }

9 public class Dwarf { private string name; private int strength; private int hitpoints; //other dwarf data public Dwarf( ); public GetDamage( ); // other dwarf methods } public class Elf { private string name; private int strength; private int hitpoints; //other elf data public Elf( ); public GetDamage( ); // other elf methods } public class Fairy { private string name; private int strength; private int hitpoints; //other fairy data public Fairy( ); public GetDamage( ); // other fairy methods } Note that all of these classes have some things in common

10 public class Dwarf { private string name; private int strength; private int hitpoints; //other dwarf data public Dwarf( ); public GetDamage( ); // other dwarf methods } public class Elf { private string name; private int strength; private int hitpoints; //other elf data public Elf( ); public GetDamage( ); // other elf methods } public class Fairy { private string name; private int strength; private int hitpoints; //other fairy data public Fairy( ); public GetDamage( ); // other fairy methods } And They Have Some Things That Are Different

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

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

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

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

15 the protected modifier (#) 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. Creature name strength hitpoints Let’s look at the base class definition public class Creature { protected string name; protected int strength; protected int hitpoints; public Creature( ) { …}; public int getDamage( ) {…};... }

16 Creature name strength hitpoints And the MagicalCreature class public class MagicalCreature : Creature { protected int spells; public MagicalCreature( ) {…};... } 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. Magical Creature spells

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 MagicalCreature is 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. Dwarf ElfFairy 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.

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

23 Creature name strength hitpoints 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: public int GetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); return damage; }

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

25 Creature name strength hitpoints 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: public int GetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); if (rgen.Next(1, 101) < 25) damage = damage + 50; return damage; } Dwarf

26 Creature name strength hitpoints Dwarf 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 int GetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); return damage; } public int GetDamage( ) { Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); if (rgen.Next(1, 101) < 25) damage = damage + 50; return damage; }

27 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. Hiding a Method in the Base Class

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

29 dw1 So, if I create a Dwarf object named dw1, and send dw1 a GetDamage( ) message... dw1.GetDamage( ) 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 dwarf Let’s let dwarves have a unique property of size. The class definition then might be: public class Dwarf : Creature { private int size; public Dwarf( ); public Dwarf(string, int, int, int); public int GetDamage( );... }

31 Now create a Dwarf object … Dwarf dw1 = new Dwarf( “Rohan”, 350, 500, 25); dw1 name strength hitPoints 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. size Creature Part Dwarf Part

32 Dwarf::Dwarf (string _nme, int _strngth, int _hpts, int _sze) : base (_nme, _strngth, _hpts) { size = _sze; } The Dwarf Constructor 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 littleCreature littleDwarf is A Dwarf object. littleCreature is a Creature reference

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

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”. joe littleDwarf base part there is no derived part. If you try to access member data in the derived part, you will get garbage!

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 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 WoodwindsBrassPercussionString

45 Violin Cello Instrument WoodwindsBrassPercussionString Kettle Drum Cymbals French Horn Trumpet Clarinet Oboe


Download ppt "Inheritance Version 1.0. Topics Inheritance Constructors and Inheritance Hiding Methods and Variables Designing with Inheritance."

Similar presentations


Ads by Google