No, not like the Eragon books… like with classes!
Imagine the following scenario:
class Goblin
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
}
class Vampire
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
}We have two different classes that have much of the same data. What if we were adding even more monsters to our game that ended up having these same stats? Seems goofy to keep adding more and more classes with duplicate info, right?
Well, we can actually do something to mitigate this duplication of code:
class Monster
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
}
class Goblin : Monster
{
}
class Vampire : Monster
{
}Whoa whoa whoa… what the heck? What’s going on here?
What we’ve done is added a third class, called Monster. We also used some fancy syntax to put that class name after the class definitions of Goblin and Vampire. This means that both Goblin and Vampire are now Monsters! Both of these classes have all the same stuff that exists inside of the Monster class:
Goblin larry = new Goblin();
Vampire dave = new Vampire();
larry.attack = 4;
dave.MaxHP = 10;Hold up tho… can we still make Monsters too? Turns out, yes! We can!
Monster charles = new Monster();
charles.gold = 500;Interesting… so what does this buy us? Well, the nice thing is that our more specific classes can still be unique:
class Monster
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
}
class Goblin : Monster
{
int frogGuts;
}
class Vampire : Monster
{
float blood;
}
So each child class can have its own stuff. But what else can we do?
Vampire dave = new Vampire();
Monster charles = dave;Wait, whaaaaaaaaaaaaat is this?
Turns out, we can use a reference of the base/parent class type to refer to a child class. This may not seem useful yet… but it will be!
OK, what else can we do? We could give our parent class, Monster, a custom constructor, like we’ve done with other classes before. That would be aweso… oh no… oooooooooooh no…

Why are we getting an error? Turns out, the parent class constructor must be called whenever we create an instance of the child class! And if there is no longer an easy default to call, then we need to explicitly call the custom one. How do we manage that? Like so:
class Monster
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
public Monster(int x)
{
gold = x;
}
}
class Goblin : Monster
{
public Goblin() : base(500)
{
}
}
class Vampire : Monster
{
public Vampire(int g) : base(g)
{
}
}Now whenever our child classes are instantiated, they also call the base class constructor! This allows everything to flow seamlessly. We can even follow how this happens in code, in real-time.
There’s also the ability to overload functions:
class Monster
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
public Monster(int x)
{
gold = x;
}
public virtual void Heal()
{
HP += 1;
}
}
class Goblin : Monster
{
public Goblin() : base(500)
{
}
}
class Vampire : Monster
{
public Vampire(int g) : base(g)
{
}
public override void Heal()
{
HP += 5;
}
}We can also make our base class abstract!
abstract class Monster
{
public int MaxHP;
public int HP;
public int attack;
public int gold;
//More stuff below here...Which causes this error:

AND we can make things protected!