Member Hiding
Tutorial
·
intermediate
·
+10XP
·
5 mins
·
(1371)
Unity Technologies

How to implement the hiding of base members in derived classes.
Languages available:
1. Member Hiding
Humanoid Class
using UnityEngine;
using System.Collections;
public class Humanoid
{
//Base version of the Yell method
public void Yell()
{
Debug.Log ("Humanoid version of the Yell() method");
}
}
Enemy Class
using UnityEngine;
using System.Collections;
public class Enemy : Humanoid
{
//This hides the Humanoid version.
new public void Yell()
{
Debug.Log ("Enemy version of the Yell() method");
}
}
Orc Class
using UnityEngine;
using System.Collections;
public class Orc : Enemy
{
//This hides the Enemy version.
new public void Yell()
{
Debug.Log ("Orc version of the Yell() method");
}
}
WarBand Class
using UnityEngine;
using System.Collections;
public class WarBand : MonoBehaviour
{
void Start ()
{
Humanoid human = new Humanoid();
Humanoid enemy = new Enemy();
Humanoid orc = new Orc();
//Notice how each Humanoid variable contains
//a reference to a different class in the
//inheritance hierarchy, yet each of them
//calls the Humanoid Yell() method.
human.Yell();
enemy.Yell();
orc.Yell();
}
}