Constructors and inheritance

An inherited class constructor must be called in a derived class. If a base class constructor contains some arguments, then it takes parameters of the derived class as input. In the following example, we will see how derived class arguments are passed in the base class constructor with inheritance:

type MyClassBase2(x: int) =   let mutable z = x * x   do for i in 1..z do printf "%d " i
type MyClassDerived2(y: int) =   inherit MyClassBase2(y * 2)   do for i in 1..y do printf "%d " i

If a class has multiple constructors, such as new(str) or new(), and this class is inherited in a derived class, we can use a base class constructor to assign values. For example, DerivedClass, which inherits BaseClass, has new(str1,str2)

Get .NET Core 2.0 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.