Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i am not using the parametarise constructor in base class without using the default constructor then there is error that base class doesn,t have the parameterless constructor .
please i need to know what is the reasons for that .

C#
public class Program
    {
        
        public Program(int i, int j)
        {
            Console.WriteLine("hello");
        }
    }


    public class Deriveclass: Program
    {


        public Deriveclass(int k, int l)
        {
        }

        private static void Main(string[] args)
        {
            class1 s = new class1(10, 3);

            Console.ReadLine();
        }


    }
Posted
Updated 9-Feb-14 17:41pm
v2

Instead of asking such questions, it would be much more useful to read some manual on programming.

Your main problem is writing gibberish like public class1(int k, int l) {}, which does not makes sense at all. There is no a class with such name, so you cannot try to write a constructor class1 for it.

Now, let's look at the problem with parameterless constructor. You just need to read compilation error information and understand it; it is quite straightforward. As soon as you created a constructor with parameters for the class Program, you loose your parameterless constructor which otherwise would be assumed for this class by default. When you create your derived class (by the way, you don't use the inheritance in any way in your derived class), you don't create any constructors at all. In contrast to the base class, it assumes that there is a parameterless constructor. One problem is implicit call of this constructor which would happen when you instantiate this class. Another rule is the implicit call of the constructor of the base class, which could only be done if your base class also had parameterless constructor, but you effectively removed it by declaring another constructor.

So, you need to modify your code to remove this inconsistency. One possibility is to add a parameterless constructor to the base class (even the one doing nothing would work):
C#
public class Program {
    public Program(int i, int j) {
        Console.WriteLine("hello"); // pointless code for a constructor, but fine as you do it for learning purpose
    }
    protected Program() {} // note that if the only purpose of it is to make your derived class work, 
                           // protected access is quite enough
}


Another possibility is to modify the derived class:
C#
public class DerivedClass {
   // of course, remove everything related to non-existing "class1"
   // ...
   // for example:
   internal DerivedClass() : base(0, 0) : { /* ... */ }
   // ... 
}


—SA
 
Share this answer
 
Comments
siddharth629 9-Feb-14 23:40pm    
sir first i am feeling sorry for "class1". and thanks for the lovely post.
Sergey Alexandrovich Kryukov 9-Feb-14 23:55pm    
You are very welcome.
Good luck, call again.
—SA
You can define another parameter less constructor in base class because f you define parameterise constructor in base class then default constructor will not create so you need to create it like:

C#
using System;
namespace Test
{
   public class Program
    {

        public Program(int i, int j)
        {
            Console.WriteLine("hello");
        }
        public Program()
        {
        }
    }


    public class Deriveclass: Program
    {
        public Deriveclass(int k, int l)
        {
        }

        private static void Main(string[] args)
        {
            Deriveclass s = new Deriveclass(10, 3);
            Console.ReadLine();
        }

    }

}



But you have another option is that call your base constructor

C#
using System;
namespace Test
{
   public class Program
    {

        public Program(int i, int j)
        {
            Console.WriteLine("hello");
        }

    }


    public class Deriveclass: Program
    {
        public Deriveclass(int k, int l):base(k,l)
        {
        }

        private static void Main(string[] args)
        {
            Deriveclass s = new Deriveclass(10, 3);
            Console.ReadLine();
        }

    }

}
 
Share this answer
 
Comments
siddharth629 9-Feb-14 23:12pm    
thanks sir ,means when ever we create a parametrise constructor then a explicitly declaration of default constructor is needed.
Sandeep Singh Shekhawat 9-Feb-14 23:33pm    
Correct dear @siddharth629

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