CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2010

    Inheriting Constructors

    Is there a way to specify in a base class that the derived class must create a constructor with certain parameters? Kind of like an abstract method, but it appears to be forbidden to create an abstract constructor.

  2. #2
    Join Date
    Apr 2007

    Re: Inheriting Constructors

    Frankly I do not understand to what use-case your train of thought is implying...
    Code is code, while runtime, it is already totally in place. There is nothing left that is "abstract" at all.

    Any extending class must implement least one of the constructors available in the super class. There is no requirement as such to implement all. Thus any extending class will inherently do what you ask "be restricted to specified set of constructors". This requirement to have a constructor as such behaves for all shits and giggles musch like an abstract method.

    The constructor must be callable, so if you say.... would create the base class in package A, have the constructors you do not want the extending classes to implement (but still want them there) as package private and have the extending classes in a different package... only the public constructor options would be accessible.

    Only protected constructors, factory methods that create instances using specific constructor and matching set of params. That way you can limit and manage which way instances of your class are actually created.
    Last edited by Londbrok; March 29th, 2011 at 04:14 AM.

  3. #3
    Join Date
    Oct 2010

    Re: Inheriting Constructors

    So, have an abstract method for initializing the fields?

  4. #4
    Join Date
    Feb 2008

    Re: Inheriting Constructors

    What he is saying is that there is no way to enforce constructor creation by an extending class.

    The 'proper' way to do it is to correctly document your Abstract class explaining that the super (params) constructor should ALWAYS be called by the base class when creating a new instance.

    Anybody who extends this class should abide by this, if not they are breaking the 'contract' that you have stated for the parent class.

    The whole point is that any constructor in an abstract class that is NOT created in the base derived classes are inaccessible, and thus serve no purpose. It's done this way for a reason: maybe one type of base class wants to use certain constructors, but another class wants to use them all.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999

    Re: Inheriting Constructors

    Quote Originally Posted by henryswanson View Post
    Is there a way to specify in a base class that the derived class must create a constructor with certain parameters? Kind of like an abstract method, but it appears to be forbidden to create an abstract constructor.
    The subclass must use a superclass constructor, so if you only provide constructors in the superclass with arguments you want to enforce, the derived classes must call one of those constructors. How they do it is not, strictly speaking, any concern of the superclass. For example, if a superclass defines only a constructor taking two arguments, then a derived class must call that constructor during instantiation, although it can do it from a no-argument constructor that provides default values for the superclass constructor:
    abstract class Foo {
        private int x;
        private String y;
        public Foo(int x, String y) {
            this.x = x;
            this.y = y;
    class Bar extends Foo {
        public Bar(int x) {    // any parameters
            super(x, "unknown"); // but superclass ctor must be called
    I can't be more specific that that without knowing why you feel an abstract constructor is necessary.

    Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
    A. Aho and J. Ullman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.