Question

santosh jha on Sat, 06 Feb 2016 11:45:17


Hi,

Need detailed example which states when we need to call the base class constructor and how we can achive the same. 

please send some example.

Thanks.


Sponsored



Replies

Alberto Poblacion on Sat, 06 Feb 2016 15:41:50


Just about all of the time you should call the base constructor, if said constructor does "something". Normally, the constructor of a class initializes some instance variables in that class. If you then inherit from the class and add some additional variables in the inherited class, then the constructor of that inherited class should initialize the new variables in the child class, and also the variables from the parent class. You achieve the latter by calling the base constructor, which already initializes those variables. This is done by appending :base()  (with arguments between the parenthesis as needed) at the end of the constructor declaration.

Example:

class Parent
{
    private int something;
    public Parent(int x)
    {
        something = x;
    }
}

class Child:Parent
{
    private int anotherVar;
    public Child(int x, int z):base(x)
    {
        anotherVar = z;
    }
}

Magnus (MM8) on Sat, 06 Feb 2016 16:39:25


The constructor of a base class is always invoked when an instance of a derived class is created:

public class Base
    {
        public Base()
        {
            //this is called first...
        }
    }

    public class Derived : Base
    {
        public Derived()
        {
            //this is called second..
        }
    }

 Derived d = new Derived();

If the base class' constructor takes arguments, you call it from a derived class using the base keyword and the following syntax:

public class Base
    {
        public Base(string arg)
        {
            //this is called first...
        }
    }

    public class Derived : Base
    {
        public Derived()
            :base("default arg...")
        {
            //this is called second..
        }

        public Dervived(string arg)
            : base(arg)
        {
        }
    }

You can only omit the base keyword from a derived class when the base class has a default parameterless constructor, but then it is being implicitly invoked anyway.

Hope that helps.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

santosh jha on Mon, 08 Feb 2016 07:27:33


Hi ,

Thanks fpr you reply and is cleared the way How to call the base constructor. but I am unable to make use of it in real life. so please provide a scenerio with example where this comes in picture. 

Thanks.

Thorsten Gudera on Mon, 08 Feb 2016 08:00:22


Hi,

create a class that derives from Adorner in WPF.

https://msdn.microsoft.com/en-us/library/ms743737%28v=vs.100%29.aspx

Or create a macro-recorder for a drawing program with a base class that already holds a Pen for a auto-redraw ("command pattern") method. Let specialized classes dervive from it...

[...]

Regards,

  Thorsten