+ All Categories
Home > Documents > Inheritance Version 1.0. Topics Inheritance Constructors and Inheritance Hiding Methods and...

Inheritance Version 1.0. Topics Inheritance Constructors and Inheritance Hiding Methods and...

Date post: 28-Dec-2015
Category:
Upload: june-dennis
View: 252 times
Download: 1 times
Share this document with a friend
Popular Tags:
45
Inheritanc e Version 1.0
Transcript

InheritanceVersion 1.0

Topics

InheritanceConstructors and InheritanceHiding Methods and VariablesDesigning with Inheritance

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

Inheritance is the act of deriving a new classfrom an already existing class. It is analogous to creating a new house blueprint from anexisting one.

Inheritance is useful when we find a naturalhierarchical relationship between classes.

Inheritance makes it possible to re-use existingcode, thus saving time and minimizing bugs.

Example and Terminology

Suppose that we are creating a newrole playing game, and we want tohave the following creatures:

dwarves elves fairies

All of these are “creatures “. They all have • A name

• A strength value• A hitpoint value

dwarves elves fairies

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}

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

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 ThingsThat Are Different

We gain a lot of productivity and functionality if we factor the common

elements out into a base class.

Creaturenamestrengthhitpoints

Dwarf Elf Fairy

Base class

Derived Class

Sometimes also called• parent class• super class

Sometimes also called• child class• sub class

This relationship is calledInheritance.

If elves and fairies are both magicalcreatures, we might envision another

layer of base and derived class

Creaturenamestrengthhitpoints

Dwarf

Base class

Derived ClassMagicalCreature

spells

Elf Fairy

Derived Class

This is known as a class hierarchy

Creaturenamestrengthhitpoints

Dwarf

MagicalCreature

spells

Elf Fairy

Dwarf inheritsfrom Creature

Elf and Fairy inheritfrom Magical Creature

Magical Creatureinherits from Creature

This is known as a class hierarchy

Creaturenamestrengthhitpoints

Dwarf

MagicalCreature

spells

Elf Fairy

Common things are definedin base classes

Unique things are definedin derived classes

the protected modifier (#) tells usthat the variable is accessiblefrom within the Creature class andfrom within any derived classes. That is,methods of the derived class can see theprotected data members defined in the base class.

Creaturenamestrengthhitpoints

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( ) {…}; . . .}

Creaturenamestrengthhitpoints

And the MagicalCreature class

public class MagicalCreature : Creature{ protected int spells;

public MagicalCreature( ) {…}; . . .}

The : means that this class inherits from theCreature class. That is, a MagicalCreatureobject will have everything that a Creatureobject has plus anything uniquely defined inThe Magical Creature class.

MagicalCreature

spells

So . . . If I create a MagicalCreatureobject 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

When data is declared as protected in a parent class, methods in a child class can see this data

ProtectedCreature

data

MagicalSpell method

But be careful … methods in the parent class cannotsee any data fields in the child part of the object.

Creature method

MagicalCreature data

This kind of inheritance is known asan 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.

Let’s look at how we deal with differences between classes by Looking at the

GetDamage( ) method.

Dwarf Elf Fairy

GetDamage( ) computes and returns the damage thatthis creature inflicts in one round of combat.

Every creature inflicts damage that is a random numberbetween 1 and the creature’s strength value.

Dwarves are good fighters, andhave a 25% chance ofinflicting an additional

50 damage points

Magical creatures can double thedamage points if they have a magic spell.

Fairies are very quick, sothey can attack twice.

Creaturenamestrengthhitpoints

Since the damage that any creature can inflictis defined as a random number between 0 and the creature’s strength value, we could writethe following code in the base Creature class:

public int GetDamage( ){ Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); return damage;}

Creaturenamestrengthhitpoints

But if we create a Dwarf object “dw1”, and invokethe GetDamage method, this method will beexecuted, because a Dwarf is-a Creature.

public int GetDamage( ){ Random rgen = new Random( ); int damage = rgen.Next(0, strength + 1); return damage;}

Dwarf

Creaturenamestrengthhitpoints

But …Dwarves have a 25% chance of inflicting an additional 50 damage points. So theGetDamage( ) 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

Creaturenamestrengthhitpoints

Dwarf

Now both the Creature class and the Dwarf classhave a method named GetDamage( ). If we havea Dwarf object “dw1”, how do we get the compilerto 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;}

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 thederived class method to tell the compiler to use this method on a derived object instead of themethod of the same name in the base class.

Hiding a Method in the Base Class

Creaturenamestrengthhitpoints

Dwarf

To Tell C# That We Want to hidethe 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-ridesthe GetDamage method in the base class byusing the keyword new.

dw1

So, if I create a Dwarf object nameddw1, and send dw1 a GetDamage( )message . . .

dw1.GetDamage( )

The GetDamage( ) method in the Dwarf class hidesthe GetDamage( ) method in the base class, and so theGetDamage( ) method in the Dwarf class gets executed.

dwarf

Let’s let dwarves have a unique propertyof 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( ); . . .}

Now create a Dwarf object …

Dwarf dw1 = new Dwarf( “Rohan”, 350, 500, 25);

dw1

name

strength

hitPoints

When the constructor is called, thecomputer looks at the Dwarf classto 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 thedata members of both the base and the derived classes. size

Creature Part

Dwarf Part

Dwarf::Dwarf (string _nme, int _strngth, int _hpts, int _sze) : base (_nme, _strngth, _hpts){ size = _sze;}

The Dwarf Constructor

Constructors are not inherited. Inorder to initialize the parent class wemust invoke the base class constructor.

If you do not explicitly call the Creatureconstructor, the default Creature constructoris called automatically.

The base class constructor finishes executing before thederived class constructor does.

Hiding Variables

If a derived class declares a variable using the same nameas 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.

Inheritance and References

Because of the is-a relationship, an object of a derivedclass can always be treated as an object of thecorresponding base class.

In particular, you can always store the reference to aderived class object in a base class reference (upcast).

Dwarf littleDwarf = new Dwarf(“Egrew”, 600, 500, 2);

Creature littleCreature;

littleCreature = littleDwarf;

littleDwarf

littleCreature

littleDwarf isA Dwarf object.

littleCreature isa Creature reference

Dwarf littleDwarf = new Dwarf(“Egrew”, 600, 500, 2);

Creature littleCreature;

littleCreature = littleDwarf;Console.WriteLine(littleCreature.Size);

littleDwarf

littleCreature

littleDwarf isA Dwarf object.

littleCreature isa Creature reference

Because littleCreature is a Creature reference you cannot directly access Dwarf data.

You can store the reference to a base class objectin a derived class reference, but you must do anexplicit cast to make this work.

Dwarf littleDwarf;Creature anyOne = new Creature(“joe”, 400, 190);

littleDwarf = (Dwarf)anyOne;

This is pretty dangerous and not often used, becausethe derived class reference thinks it is referencing aderived class object, but it really isn’t. This isreferred to as the “slicing problem”.

joe

littleDwarf

basepart

there is no derived part.If you try to access memberdata in the derived part, youwill get garbage!

Upcasting vs. Downcasting

Casting from a descendant type to an ancestor type is known as upcasting. It is always safe, since you aremoving up the inheritance hierarchy. In our case, forexample, we are always know that a dwarf is an creature.

Casting from an ancestor type to a descendant typeis called downcasting. In our case, we can’t guaranteethat every creature is a dwarf, so downcasting a creature to a dwarf can be very dangerous, sincewe assume that information may be there that isn’t.

Designing With Inheritance

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.

Designing With Inheritance

For example, suppose that we wanted to writea program that deals with instruments in anorchestra

ViolinOboeViolaClarinetKettle Drum . . .

Designing With Inheritance

Now design a class that contains all of thethings that orchestra instruments have incommon.

* Instrument name* Owner* Where it is in the orchestra

Then write methods to manage this data

Designing With Inheritance

Look at all of the instruments in the orchestra.can they be organized into some different groupswhere each group has some things in common.

Instrument

Woodwinds Brass Percussion String

Violin

Cello

Instrument

Woodwinds Brass Percussion String

Kettle Drum

Cymbals

French Horn

Trumpet

Clarinet

Oboe


Recommended