Passing parameters to the base class constructor
36,048
Like this:
public class DerivedClass : BaseClass
{
public DerivedClass(int derivedParam, String baseParam):base(baseParam)
{
}
}
The base
keyword here calls the base class constructor that matches the provided parameter overload.
Related videos on Youtube
Author by
n.y
Bachelor's degree, Computer Software Engineering. Master’s degree, Information Security. Currently a Programmer.
Updated on September 09, 2020Comments
-
n.y almost 4 years
If the base class and derived class both have their constructors with parameters then where we pass the parameters to the base class constructors?
-
catfood about 10 yearsThis is a duplicate question. stackoverflow.com/questions/12051/… Googling "base class constructor parameters" would have brought it right to you.
-
phoog about 10 yearsYou say "you can't instantiate an abstract class," but that's not entirely true. If you create an instance of the non-abstract derived class, that instance is also an instance of the abstract base class. It is also an instance of the System.Object class.
-
Chris Sinclair about 10 yearsFYI, even if it's an
abstract
class, it can have defined custom constructors with parameters that can be passed in. They can't be instantiated/called directly but instead are referenced in derived types by using thebase
keyword with the derived constructors.
-
-
Ahmn21 about 4 yearsIs the default constructor still invoked if base constructor is called as
:base(param)
? -
BradleyDotNET about 4 yearsNo. That constructor would have to call
this()
@Ahmn21