class Person
{
public int age;
public Person()
{
age = 1;
}
}
class Customer : Person
{
public Customer()
{
age += 1;
}
In c# using base and derived classes THERE MUST BE SOME IMPLICIT OR EXPLICIT CALL TO SOME CONSTRUCTOR IN THE BASE CLASS FROM THE DERIVED CLASS.
I didn't understand how all this worked until I realized that fact.
In other words, when you connect a base class to a derived class, some constructor must be called in the base class from the derived. The base class is always instantiated first from the derived class via a call to some constructor in the base class. C# doesn't care if it is a default constructor or non-default constructor with parameters. That is why you can leave out a default constructor in all your classes as its called implicitly ONLY IF no other non-constructor with parameter(s) is added in the base class.
When you suddenly add a non-default constructor with parameter(s), it breaks the default hidden default constructor chain creation and calls. In your Base class with a non-default constructor you must now either call that constructor explicitly from the derived class or add a default constructor explicitly in the base class.
Let's test this.....
// THIS WORKS!!!
class MyBaseClass0
{
// no default constructor - created automatically for you
}
class DerivedClass0 : MyBaseClass0
{
// no default constructor - created automatically for you and calls the base class default constructor above
}
// THIS WORKS!!!
class MyBaseClass1
{
// same as above
}
class DerivedClass1 : MyBaseClass1
{
public DerivedClass1()
{
// here the derived class default constructor is created explicitly but the call to the base class default constructor is implicitly called
}
}
// AND THIS WORKS!!!
class MyBaseClass2
{
// as above
}
class DerivedClass2 : MyBaseClass2
{
public DerivedClass2() : base()
{
// here we explicitly call the default constructor in the base class using base(). note its optional as base constructor would be called anyway here
}
}
// AND THIS WORKS!!!
class MyBaseClass3
{
// no default constructor
}
class DerivedClass3 : MyBaseClass3
{
public DerivedClass3(int x)//non-default constructor
{
// as above, the default constructor in the base class is called behind the scenes implicitly here
}
}
// AND THIS WORKS
class MyBaseClass4
{
// non default constructor but missing default constructor
public MyBaseClass4(string y)
{
}
}
class DerivedClass4 : MyBaseClass4
{
// non default constructor but missing default constructor
public DerivedClass4(int x) : base("hello")
{
// note that here, we have fulfilled the requirement that some constructor be called in base even if its not default
}
}
// BUT THIS FAILS!!!...until you either add in a base() call to the non-default constructor or add in the default constructor into base!
class MyBaseClass5
{
// 1. EITHER ADD MISSING DEFAULT CONSTRUCTOR HERE AND CALL IT USING base() below....
public MyBaseClass5() { }
// 2. Or use the non-default constructor and call to base("hello") below
//public MyBaseClass5(string y)
//{
//}
}
class DerivedClass5 : MyBaseClass5
{
public DerivedClass5(int x) : base()// 1. Either ADD explicit call here to explicit default constructor in base class
{
}
//public DerivedClass5(int x) : base("hello")// 2. Or ADD explicit call here to parameter-based constructor in base class
//{
//}
}
The reason all the items above work is either: 1. The call to the default constructor in the base class is implicitly created in the base class and implicitly called from the derived because no non-default constructor has been added to the base class or 2. There is an explicit call to non-default , parameter-based constructor using base(myparamter)