Muhammad Imran Ansari
Is it possible for a child class to inherit the constructor of its base class?

Is it possible for a child class to inherit the constructor of its base class?

By Muhammad Imran Ansari in OOP/OOD on Jul 21 2022
  • Abhishek Khandare
    Sep, 2022 27

    The answer is NO
    we cannot inherit it but we can invoke it. first of all we should know what inheritance can do so in inheritance we can overrides parent class’s members. but constructor is not a memeber of a class.

    When we are using the constructors in the inheritance, Base class constructors are accessible to the child class hence when we create an object for the child class, constructors of both parent and child class get executed.

    1. using System;
    2. public class BaseClass
    3. {
    4. private int y=12;
    5. public BaseClass()
    6. {
    7. Console.WriteLine ("BaseClass Constuctor "+this.y+"\n");
    8. }
    9. };
    10. public class ChildClass:BaseClass
    11. {
    12. public ChildClass()
    13. {
    14. Console.WriteLine ("Childclass constructor\n");
    15. }
    16. };
    17. public class HelloWorld
    18. {
    19. public static void Main(string[] args)
    20. {
    21. ChildClass x = new ChildClass();
    22. Console.WriteLine ("Hello World");
    23. }
    24. };

    Inheritance in Parametrized Constructor
    In the case of the default constructor, it is implicitly accessible from parent to the child class but parameterized constructors are not accessible to the derived class automatically, for this reason, an explicit call has to be made in the child class constructor to access the parameterized constructor of the parent class to the child class using the following syntax.

    1. using System;
    2. public class BaseClass
    3. {
    4. private int y;
    5. public BaseClass(int x)
    6. {
    7. this.y=x;
    8. Console.WriteLine ("BaseClass Constuctor "+this.y+"\n");
    9. }
    10. };
    11. public class ChildClass: BaseClass
    12. {
    13. public ChildClass():base(56)
    14. {
    15. Console.WriteLine ("Childclass constructor\n");
    16. }
    17. };
    18. public class HelloWorld
    19. {
    20. public static void Main(string[] args)
    21. {
    22. ChildClass x = new ChildClass();
    23. Console.WriteLine ("Hello World");
    24. }
    25. };

    But overall we are invoking contructor here not inheriting and eventhough we don’t need to overrides inside the constructors as it’s use for the initiaise purpose if we require we do change in base class constuctor directly or another constructor inside the base class.

    As you can see below.

    1. using System;
    2. public class BaseClass
    3. {
    4. protected int y;
    5. public int addition(int x)
    6. {
    7. x++;
    8. y = x;
    9. return (y);
    10. }
    11. };
    12. public class ChildClass: BaseClass
    13. {
    14. };
    15. public class HelloWorld
    16. {
    17. public static void Main(string[] args)
    18. {
    19. ChildClass x = new ChildClass();
    20. Console.WriteLine (x.addition(5));
    21. }
    22. };

    object of a child class x uses method of addition from base class and give you an output : 6
    But when you write same method having same parameter with diffenent logic inside a child class. it overrides it.

    1. using System;
    2. public class BaseClass
    3. {
    4. protected int y;
    5. public int addition(int x)
    6. {
    7. x++;
    8. y = x;
    9. return (y);
    10. }
    11. };
    12. public class ChildClass:BaseClass
    13. {
    14. public int addition(int x )
    15. {
    16. y = x;
    17. return (--y);
    18. }
    19. };
    20. public class HelloWorld
    21. {
    22. public static void Main(string[] args)
    23. {
    24. ChildClass x = new ChildClass();
    25. Console.WriteLine (x.addition(5));
    26. }
    27. };

    and give you an output:4

    So overall we cannot inherit constructor of a base class but we can invoke it.

    • 1
  • Umar Riaz
    Nov, 2023 27

    yes

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS