I have a class (let’s call it Class1), which inherits another class (Class2).
What I’m trying to do is run the costructor method in Class2 upon inheritance in Class1.
I suppose it has to look something like this:
// Class1
public class Class1 : Class2
{
public Class1()
{
}
}
// Class2
public class Class2
{
public Class2()
{
//this is the function I want to run
}
}
But I can’t get it to work like this.
Is there a way to do this, without actually having to call the Class2() constructor method from Class1?
You’re declaring your constructors incorrectly. Remove the
voidkeyword:The default constructor of the base class will run automatically. If you wish to call a non-default constructor, you can do it like this: