Thread: Constructor Call Question

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Constructor Call Question

    Code:
    using System;
    
    namespace myProgram
    {
    
        class BaseClass
        {
            public BaseClass()
            {
                Console.WriteLine("Base Class Constructor has been called!");
            }
        }
    
        class DerivedClass : BaseClass
        {
            public DerivedClass()
            {
                Console.WriteLine("Derived Class Constructor has been called!");
            }
        }
    
        class Program
        {
            static void Main()
            {
                DerivedClass myObj = new DerivedClass();
            }
        }
    }
    When making an object of the Derived Class the program calls the constructors from both the base class and the derived class. Is there anyway I can make use of the derived class as an object without calling the constructor from its base class?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    49
    Base classes should not include anything you would not want in the derived class. You can however override methods in the base class with methods in the derived class.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    This is the inheritance effect. DerivedClass is BaseClass, so it will do everything BaseClass does unless you override the method. Constructors can be overriden but only if they are not the default constructor. In this example you have shown it will always run the BaseClass constructor.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by dhuan View Post
    Is there anyway I can make use of the derived class as an object without calling the constructor from its base class?
    Why would you want to do this? What problem are you trying to solve?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Read here

    I believe that a :base() is added to your constructor. Thus
    Code:
    class DerivedClass : BaseClass
        {
            public DerivedClass() :base()
            {
                Console.WriteLine("Derived Class Constructor has been called!");
            }
        }
    thus you cannot avoid calling the base constructor. This should be done in all constructors that don't specify an initializer.

    What you would do is have a base class with an empty constructor. And derive from there. In your case you could have a BaseBaseClass() the same as BaseClass() with an empty constructor. Preferably it will be an abstract class since you won't want to use it.

    Eh, of course you can get tricky and specify two constructors in the base class. One can be
    Code:
    public BaseClass(int useless) {}
    and now you can do
    Code:
     class DerivedClass : BaseClass
        {
            public DerivedClass() : base(0)
            {
                Console.WriteLine("Derived Class Constructor has been called!");
            }
        }
    and call the empty constructor.

    The best is the first method (base abstract class). This implies good class planning. But you can use a workaround in case you already have planned/coded your classes and you just need to do this modification.

    The reason this happens is that the compiler needs to call a constructor for the base class so what you want is to have an empty constructor and then it is just a matter of coding it nicely to call that empty constructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM