Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are derived classes constructors forced to use base keyword?

Tags:

c#

constructor

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 ?

like image 344
Veverke Avatar asked Jul 19 '16 14:07

Veverke


People also ask

Is it mandatory to create constructor in derived class if base class contains constructor?

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.

When used in a derived class which keyword refers to the base class?

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.

Why is the constructor of a base class called when a derived object is created?

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 can a base class constructor be used as a 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++


2 Answers

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.

like image 137
Servy Avatar answered Oct 11 '22 12:10

Servy


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) { }
 }
like image 30
Andrew Skirrow Avatar answered Oct 11 '22 13:10

Andrew Skirrow