Is it possible for a child class to inherit the constructor of its base class?
The answer is NOwe 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.
using System;public class BaseClass{ private int y=12; public BaseClass() { Console.WriteLine ("BaseClass Constuctor "+this.y+"\n"); }};public class ChildClass:BaseClass{ public ChildClass() { Console.WriteLine ("Childclass constructor\n"); }};public class HelloWorld{ public static void Main(string[] args) { ChildClass x = new ChildClass(); Console.WriteLine ("Hello World"); }};
using System;
public class BaseClass
{
private int y=12;
public BaseClass()
Console.WriteLine ("BaseClass Constuctor "+this.y+"\n");
}
};
public class ChildClass:BaseClass
public ChildClass()
Console.WriteLine ("Childclass constructor\n");
public class HelloWorld
public static void Main(string[] args)
ChildClass x = new ChildClass();
Console.WriteLine ("Hello World");
Inheritance in Parametrized ConstructorIn 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.
using System;public class BaseClass{ private int y; public BaseClass(int x) { this.y=x; Console.WriteLine ("BaseClass Constuctor "+this.y+"\n"); }};public class ChildClass: BaseClass{ public ChildClass():base(56) { Console.WriteLine ("Childclass constructor\n"); }};public class HelloWorld{ public static void Main(string[] args) { ChildClass x = new ChildClass(); Console.WriteLine ("Hello World"); }};
private int y;
public BaseClass(int x)
this.y=x;
public class ChildClass: BaseClass
public ChildClass():base(56)
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.
using System;public class BaseClass{ protected int y; public int addition(int x) { x++; y = x; return (y); }};public class ChildClass: BaseClass{};public class HelloWorld{ public static void Main(string[] args) { ChildClass x = new ChildClass(); Console.WriteLine (x.addition(5)); }};
protected int y;
public int addition(int x)
x++;
y = x;
return (y);
Console.WriteLine (x.addition(5));
object of a child class x uses method of addition from base class and give you an output : 6But when you write same method having same parameter with diffenent logic inside a child class. it overrides it.
using System;public class BaseClass{ protected int y; public int addition(int x) { x++; y = x; return (y); }};public class ChildClass:BaseClass{ public int addition(int x ) { y = x; return (--y); }};public class HelloWorld{ public static void Main(string[] args) { ChildClass x = new ChildClass(); Console.WriteLine (x.addition(5)); }};
public int addition(int x )
return (--y);
and give you an output:4
So overall we cannot inherit constructor of a base class but we can invoke it.
yes