I have an abstract
class with constructor XYZ(string name)
.
Also I have a class that inherits from that abstract class.
How to force inherited class to call base(string name)
?
Now I can use new Inherited()
and it will not call base constructor. I want to force user to implement default constructor in inherited class.
yes it is. And a constructor of abstract class is called when an instance of a inherited class is created.
For multiple inheritance order of constructor call is, the base class's constructors are called in the order of inheritance and then the derived class's constructor.
In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.
An abstract class cannot be inherited by structures. It can contain constructors or destructors.
As we all know abstract classes also do have a constructor. So if we do not define any constructor inside the abstract class then JVM (Java Virtual Machine) will give a default constructor to the abstract class.
So a constructor from a subclass can call the constructor of its abstract superclass using super(...). For example: public class Abstract { protected Abstract(int x) { } } public class Concrete { public Concrete(int x, int y) { super(x); // Call the superclass constructor } }
Constructor: Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. An abstract class also has a constructor. if we don’t define any constructor inside the abstract class then JVM (Java Virtual Machine) will give a default constructor to the abstract class.
Make parameterless constructor in your abstract class private, or not add it at all. That's will force all derived classes to call the constructor you specified or there will be a compile error.
A class without an explicit constructor has a parameterless constructor. In the other hand, if you implement a constructor with parameters and no paramterless constructor, your class won't be instantiable without arguments.
In other words:
public abstract class A
{
public A(string x)
{
}
}
public class B : A
{
// If you don't add ": base(x)"
// your code won't compile, because A has a
// constructor with parameters!
public B(string x) : base(x)
{
}
}
That is, if A
has a parameterless constructor (or no explicit constructor), B
will automatically call the base constructor. You don't need to code any further stuff here.
Otherwise, if your base class has a parameterless constructor and a constructor with parameters, you can't force a derived class to automatically call a constructor excepting the default one (i.e. the so-called parameterless constructor).
Well, there's no special workaround here, but be aware C# supports optional parameters in both constructors and methods.
If you want to be 100% sure derived classes will call a concrete base constructor, you can implement your base class using a single parameterless constructor with optional parameters and use this instead of constructor overloading:
public class A
{
public A(string x = "hello world") // or just string x = null
{
}
}
Now if a B
class derived A
, B
will always call A
's base constructor, since x
is optional and it has a default value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With