Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
case 1:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        public Baseclass(int a)
        {
            Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        }
    }

    class Derivedclass:Baseclass
    {

        //public Derivedclass()
        //{

        //}

        public Derivedclass(int a)
            : base(a)
        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}



--Now this gives me output
Value of Baseclass a is : 15
Value of Derivedclass a is :5

-----------------------------------------
case 2 :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        public Baseclass(int a)
        {
            Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        }
    }

    class Derivedclass:Baseclass
    {

        public Derivedclass()
        {

        }

        public Derivedclass(int a)
            : base(a)
        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}


--Now this gives me error
'check.Baseclass' does not contain a constructor that takes '0' arguments

-------------------------------------------------------------------------------
case 3:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        //public Baseclass(int a)
        //{
        //    Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        //}
    }

    class Derivedclass:Baseclass
    {

        public Derivedclass()
        {

        }

        public Derivedclass(int a)

        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}



Now this gives me output:
Value of Derived class a is : 5

--------------------------------------

case 4:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        public Baseclass(int a)
        {
            Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        }
    }

    class Derivedclass:Baseclass
    {

        //public Derivedclass()
        //{
        //    Console.WriteLine("anurag");

        //}

        public Derivedclass(int a)

        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}



o/p
This gives me error
--------------------------------------

1)
a)if we are creating parametrized constructor of baseclass.
b)And if we try to create default constructor of derived class
c)And if no default constructor of base class
d)creating object of derived class

it is showing error.

2)
a)If no constructor of baseclass is there
b)If default constructor of derived class is there
c)creating a object of derived class.

then i dont get error

So is it like when we create parametrized constructor of baseclass , then it looks for default constructor only and only if derived class constructor is there ?

Kindly explain why it is happening in such a way ?
Posted
Updated 24-Jan-14 6:14am
v2

1) When you created single parameter constructor in base class. You are actually telling there is only one way to initialize your base class and it should have a parameter.Thus All child class constructor must call its base class constructor with this parameter.
 
Share this answer
 
Comments
anurag19289 24-Jan-14 11:47am    
I m not clear with the sentence. Kindly elucidate.

Like can you explain in case 2 of my question where i am getting error.
i have created single parameter constructor in base class. And derived class default constructor is there.
Matt T Heffron 24-Jan-14 13:16pm    
Any Derivedclass constructor MUST call some constructor for the Baseclass.
Because the Baseclass has only the 1 parameter constructor, you must specify that constructor from the Derivedclass default constructor.
Also, since you have declared ANY constructor for the Baseclass, the compiler will NOT auto-generate a parameterless default constructor for the class.
The Baseclass therefor has NO default constructor so the Derivedclass must use the parameter constructor of the Baseclass.
If you don't specify a specific :base constructor, it is equivalent to :base() i.e., specifying the default constructor of the base class.
Since there is no default constructor for Baseclass, you get the error.
anurag19289 24-Jan-14 13:26pm    
Now everything is clear. Simply Superb.

-->So compiler gernerates the parameterless constructor only if there is no other overloaded constructor

--> And you are right :) if we dont specity :base it is equivalent to base(). No default constructor. So error.

Kindly write this in solution I will accept the answer :) Thank you very much.
... edit #1 ...

The OP asked why case #4, in which both base class and derived class each have one constructor with matching parameter signatures, will not compile, throwing an error message: "does not contain a constructor that takes 0 arguments."

In case #4, the constructor in the derived class does not call :base(a) ... so ... the compiler looks for a parameterless constructor for the base class, and throws an error when it does not find one.

imho, here is a case where the "why" of how C# behaves reflects the choices of the language designers, just as allowing a base class with no constructors, and only function definitions ... as shown below ... to work fine with all derived classes no matter what constructors they implement is a design choice.

Whether we like it or not, whether we call such design choices "arbitrary," or "enlightened," love them, or hate them: that's the way the language works.

... end edit #1 ...

I would explain this in two ways: consider this definition of 'Baseclass:
C#
public class Baseclass
{
    public double hyp(int x, int y)
    {
        return (Math.Sqrt((x * x) + (y * y)));
    }
}
Here there are no (formal) constructors defined. Any class that inherits from 'Baseclass can define constructors (as many as you like) with different numbers of parameters:
C#
public class Derivedclass: Baseclass
{
    public double hypo { get; set; }

    public Derivedclass(int x, int y)
    {
        hypo = this.hyp(x,y);
    }
}
Notice here that there is no call to :base in the definition of 'Derivedclass. In essence, 'Baseclass acts as a repository for a function 'hyp: and, as you see, the function can be invoked without calling a constructor in 'Baseclass in the usual way.

Once you start using calls to :base in a derived class, then, in general, you should make sure you have constructors in the base class whose signature match each of the constructors in 'Derivedclass, and, as you found out, you must provide a parameterless constructor in 'BaseClass.

What you put in a base class, what data, what methods, should be carefully selected to meet the needs of your application. Having what appears to be a never-used parameterless constructor hanging there, in the base class, will not burn-up your cpu !

Let's just say that, like a good parent, a base class, at times, yearns to hear from its children :)
 
Share this answer
 
v3
Comments
anurag19289 24-Jan-14 12:24pm    
Once you start using calls to :base in a derived class, then, in general, you should make sure you have constructors in the base class whose signature match each of the constructors in 'Derivedclass, and, as you found out, you must provide a parameterless constructor in 'BaseClass.

--ok in case 2 of the question. I was missing the parameterless constructor of baseclass. So I got the error.

--------------------------------------------------------------------------------
Ok if I see case:4 of my question. I am not using base keyword. Then why did I get error here.

As per you

"in general, you should make sure you have constructors in the base class whose signature match each of the constructors in 'Derivedclass'"
which is matching
BillWoodruff 24-Jan-14 20:53pm    
I have edited my reply above; let me know if what I added explains case #4 satisfactorily.
Ron Beyer 24-Jan-14 21:11pm    
Good explanation Bill, +5!
Karthik_Mahalingam 24-Jan-14 21:39pm    
5! Good
anurag19289 24-Jan-14 23:11pm    
Thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900