Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# inheritance and default constructors

Suppose there is a base class A and a class B derived from A. Then, we know that the constructor of class A is never inherited by class B. However, when a new object of B is created, then - the default constructor of the class A is called prior to the default/custom constructor of class B is invoked. Maybe the purpose of this is that the fields of class A need to be initialized to default values.

Now, suppose that class A has defined a custom constructor. This means that the default constructor of class A is silently removed by the compiler. Now, on creating a new instance of class B, which constructor of class A is automatically called before invoking the class B's constructor? (How does the class A fields get initialized in such a case?)

like image 703
helloworld Avatar asked Apr 30 '15 05:04

helloworld


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Now, on creating a new instance of class B, which constructor of class A is automatically called before invoking the class B constructor?

The code will fail to compile, basically. Each constructor has to chain to another constructor, either implicitly or explicitly. The constructor it chains to can be in the same class (with this) or the base class (with base).

A constructor like this:

public B() {} 

is implicitly:

public B() : base() {} 

... and if you don't specify a constructor at all, it will be implicitly added in the same way - but it still has to have something to call. So for example, your scenario:

public class A {     public A(int x) {} }  public class B : A {} 

leads to a compiler error of:

error CS7036: There is no argument given that corresponds to the required formal parameter 'x' of 'A.A(int)'

However, you can specify a different constructor call explicitly, e.g.

public B() : base(10) {} // Chain to base class constructor 

or

public B() : this(10) {} // Chain to same class constructor, assuming one exists 
like image 85
Jon Skeet Avatar answered Sep 28 '22 18:09

Jon Skeet


Once you provide your own constructor to class A, no automatic invocations happens during the class B object creation.

The first line in your class B constructor should be super(paramsToClassAConstructor) or it can be call to another constructor with in the class B using this(). It is the responsibility of the second constructor in class B to call the class A constructor in such case.

like image 41
Bhargav Kumar R Avatar answered Sep 28 '22 18:09

Bhargav Kumar R