Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class with constructor, force inherited class to call it

Tags:

c#

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.

like image 795
apocalypse Avatar asked Feb 17 '15 18:02

apocalypse


People also ask

Can constructors be inherited in abstract class?

yes it is. And a constructor of abstract class is called when an instance of a inherited class is created.

How do you call a constructor from an inherited class?

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.

Do inherited classes inherit constructors?

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.

Can inherited class be abstract?

An abstract class cannot be inherited by structures. It can contain constructors or destructors.

Do abstract classes also have constructors?

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.

How to call the constructor of an abstract superclass from a subclass?

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 } }

What is constructor in Java?

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.

How to prevent all derived classes from calling a constructor?

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.


1 Answers

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).

Workaround

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.

like image 182
Matías Fidemraizer Avatar answered Nov 10 '22 15:11

Matías Fidemraizer