Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. If a class can call its base constructor then why the constructor is not inherited

If a class can call its base constructor then why the constructor is not inherited

Scheduled Pinned Locked Moved C#
oop
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CRobert456
    wrote on last edited by
    #1

    I miss the logic of the official story that a base constructor is not inherited. If a class is able to call its base constructor (accessible) then why it is said the constructor is not inherited. The logic seems to be based on the fact that a base constructor has to be called explicitly, meaning the compiler will not call it automatically (ignore special cases). But the definition of inheritance is not based on the automatic calling.

    P L 2 Replies Last reply
    0
    • C CRobert456

      I miss the logic of the official story that a base constructor is not inherited. If a class is able to call its base constructor (accessible) then why it is said the constructor is not inherited. The logic seems to be based on the fact that a base constructor has to be called explicitly, meaning the compiler will not call it automatically (ignore special cases). But the definition of inheritance is not based on the automatic calling.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]

      C M D 3 Replies Last reply
      0
      • P PIEBALDconsult

        In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]

        C Offline
        C Offline
        CRobert456
        wrote on last edited by
        #3

        The question assumes an instance constructor. A static constructor is not meant to be called by any outside code or any other class (derived or not derived), it is called only once by the loader, so the issue of derivation is totally moot for static constructor.

        P 1 Reply Last reply
        0
        • C CRobert456

          The question assumes an instance constructor. A static constructor is not meant to be called by any outside code or any other class (derived or not derived), it is called only once by the loader, so the issue of derivation is totally moot for static constructor.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          And my answer applies to instance constructors -- in C#.

          1 Reply Last reply
          0
          • C CRobert456

            I miss the logic of the official story that a base constructor is not inherited. If a class is able to call its base constructor (accessible) then why it is said the constructor is not inherited. The logic seems to be based on the fact that a base constructor has to be called explicitly, meaning the compiler will not call it automatically (ignore special cases). But the definition of inheritance is not based on the automatic calling.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            CRobert456 wrote:

            The logic is based on the fact the a base constructor has to be called explicitly, meaning the compiler will not call it automatically

            That would mean that all objects (!) would inherit the default constructor. That'd be a bit impractical. You can add code to call an inherited constructor in your class, even if there's a different signature.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            1 Reply Last reply
            0
            • P PIEBALDconsult

              In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]

              M Offline
              M Offline
              Matt T Heffron
              wrote on last edited by
              #6

              That just sounds wrong. From the C# 5 lang spec: section 1.6.6.3

              The ... constructor (which is like an instance method) ...
              Because the constructor is an instance member, it is permitted to access both the ... instance field and the ... static field.

              The constructor has access to all of the instance's fields and properties and even instance methods. Section 1.6.7.1

              Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.

              Section 10.11.1:

              If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. Thus, an instance constructor declaration of the form
              C(...) {...}
              is exactly equivalent to
              C(...): base() {...}

              A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]

                D Offline
                D Offline
                David A Gray
                wrote on last edited by
                #7

                As I recall, the default base ctor is always called first, at least it is if the base class contains instance properties that need initialization.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups