C# 6.0 in a Nutshell by Joseph Albahari and Ben Albahari (O’Reilly).
Copyright 2016 Joseph Albahari and Ben Albahari, 978-1-491-92706-9.
states the following at page 96, after introducing Implicit Calling of Parameterless Base-Class Constructor:
If the base class has no accessible parameterless constructor, subclasses are forced to use the
base
keyword in their constructors.
I am trying to create a code snippet to corroborate that, but did not yet succeed.
My snippet:
public class X
{
public int Num { get; set; }
public void Method_1()
{
Console.WriteLine("X");
}
public virtual void Method_2()
{
Console.WriteLine(Num);
}
}
public class Y : X
{
public Y()
{
Num = 1000;
}
}
private static void Main(string[] args)
{
new Y().Method_2();
}
I expected to get a compiler error, following the book affirmation, but I get none. The code runs and correctly prints 1000.
My question is: what does the book mean with subclasses being forced to use the base keyword ? I am trying to reproduce such scenario.
What am I missing ?
However, if a base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor.
1. Accessing base class variables. When we have the data members of the same name in both the base and derived class, we can use the super keyword to access the base class member, data , in the derived class.
A technical reason for this construction order is that compilers typically initialize the data needed for polymorphism (vtable pointers) in constructors. So first a base class constructor initializes this for its class, then the derived class constructor overwrites this data for the derived class.
How to call the parameterized constructor of base class in derived class constructor? To call the parameterized constructor of base class when derived class's parameterized constructor is called, you have to explicitly specify the base class's parameterized constructor in derived class as shown in below program: C++
The class Y
does have access to a parameterless constructor for the base class, X
, since if a class defines no constructor there is an implicit parameterless constructor.
If you write:
public class X
{
public X(int i){}
}
Then there will no longer be an accessible parameterless constructor of X
in Y
.
The problem stems for a misunderstanding of your example code. Because you've not defined any constructor in class X
C# has defined an implicit one for you. This implicit constructor is a no-arg constructor.
The quote you're mentioning refers to the case where you've actually written a non-default constructor.
Writing the non-default constructor suppresses the generation of an implicit constuctor. This means you're forced to explicitly call another constructor from the constructor of the Dervived
class using the base
keyword
class BaseClass
{
public BaseClass(int i) {}
}
class Derived : BaseClass
{
public Derived() : base(44) { }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With