Please login to save your progress.

Inheritance

Inherited Methods and Properties

Being able to inherit from another type has limited uses if that's all there was to it. Luckily there is a lot more to inheritance than simply saying x is y. Inheritance allows us to share methods, properties, fields and constructors through our inheritance hierachy. Let's look at an example:

 

In our example above, we've defined two types, the Animal class, and the Dog class. On the Animal class, we then define a method, Speak. Being a public method on a base class, this makes it available to all derived classes. This is the same for methods declared on Object, so you'll notice that we can call ToString() on our Dog class, because it is inherited through System.Object.

This same mechanism can also be applied to other type members, including properties.

What we are declaring with this mechanism, is that if I know x is a y, then I know that I can write x.Name, if y declares Name

Page 7 of 18