Ali Adlavaran
Ali Adlavaran

Reputation: 3735

Storing the object of derived class as a base class variable. What is OOP term for this ?

In C#, we often use a initializing syntax that a instance of a base class is initializing with its inherited child class like this:

Control BTN = new Button();

Now my question is just that, what is the formal name of this kind of initializing?

Upvotes: 2

Views: 748

Answers (3)

Mark Byers
Mark Byers

Reputation: 838716

According to the C# specification, it is called simple assignment with an implicit reference conversion.

7.16.1 Simple assignment

In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand.

...

The run-time processing of a simple assignment of the form x = y consists of the following steps:

  • ...
  • y is evaluated and, if required, converted to the type of x through an implicit conversion (§6.1).

6.1.6 Implicit reference conversions

The implicit reference conversions are:

  • ...
  • From any class-type S to any class-type T, provided S is derived from T.

Upvotes: 6

Dominic P
Dominic P

Reputation: 2901

The most concise term I can think of is polymorphic instantiation, or polymorphic initialization. That seems to capture the necessary elements without writing a whole sentence. Not aware of a formal term, though.

Upvotes: 3

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

This is not base class instance initializing. This is the inherited class instance initializing and referencing this instance via variable of base class type. You can call it assignment value to variable. No special name.

Upvotes: 0

Related Questions