0

I'm reading a beginner's C# book. Everything was smooth defining objects like this:

BaseClass foo = new BaseClass();

but then the author, without any explanation, changed the definitions like this:

MiClass foo = new DerivedClass();

I would like to learn about this in a book or internet but I don't know what words to use to search for this topic.

user3646717
  • 1,095
  • 2
  • 12
  • 21

1 Answers1

6

The term for this is subtyping, or substitution.

When two types have an inheritance relationship, one is said to be a subtype and the other is the supertype. When this is applicable, a subtype instance can be used in a context where a supertype instance was expected (the context being a local variable, a field, a parameter, etc).

This is why you may often hear that inheritance denotes an is-a relationship. Compare with composition (the practice of including an object as a member of another object), which denotes a has-a relationship (and is the other way of achieving code reuse when dealing with classes).

Back to inheritance, if your classes are defined like this, for example:

class Banana : Fruit { ... }

We'd say that Banana (apart from being a Banana, obviously) is a Fruit as well. Because of that relationship, you can do this, obviously:

Banana obj = new Banana();

But you do this as well:

Fruit obj = new Banana();

And you can do something similar whenever a field or a method parameter expects a Fruit.

This type of relationship is useful when you want parts of your code to use an object, but you don't want them to know too many details about that object (maybe because those details are irrelevant or subject to change). If those parts can do their job with less specific information (in this case, the fact that the object is some kind of Fruit and not specifically a Banana), then it's preferable to have them work with that. This is a form of decoupling and it's a generally desired property of code as a project gets more and more complex.

Community
  • 1
  • 1
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • Thank you. The book did explained polymorphism and inheritance but never mentioned anything about a definition such as Fruit obj = new Banana(); That is what I didn't even know how to name. – user3646717 Jan 13 '16 at 17:33