C
C#2y ago
Avis

How do I call the constructor of a parent class?

If I have something like
abstract class parent {
public parent(string mom, string dad) {...}
}

public class child : parent {
//how do I cal the constructor of parent?
}
abstract class parent {
public parent(string mom, string dad) {...}
}

public class child : parent {
//how do I cal the constructor of parent?
}
All of the tutorials i've found only talk about inheriting a class.
15 Replies
vdvman1
vdvman12y ago
abstract class Parent
{
public Parent(string mom, string dad)
{
...
}
}

public class Child : Parent
{
public Child(string mom, string dad)
: base(mom, dad)
{
...
}
}
abstract class Parent
{
public Parent(string mom, string dad)
{
...
}
}

public class Child : Parent
{
public Child(string mom, string dad)
: base(mom, dad)
{
...
}
}
b14me
b14me2y ago
Public child() : base(“mom”, “dad”)
Avis
Avis2y ago
so base works as the parent class's constructor?
Aaron
Aaron2y ago
this seems like a weird thing to represent with inheritance
x0rld
x0rld2y ago
base call the parent constructor
Aaron
Aaron2y ago
yes
Avis
Avis2y ago
thank you!
b14me
b14me2y ago
As the parent in general. You can use ‘base.ParentMethod()’ for e.g. just check the docs on keyword
Avis
Avis2y ago
quick question wouldn't this make a second child class inside the first?
vdvman1
vdvman12y ago
What makes you say that?
Avis
Avis2y ago
so Parent Child Child
x0rld
x0rld2y ago
nop unless you have static
Avis
Avis2y ago
public class Child : Parent // make a child class
{
public Child(string mom, string dad)
: base(mom, dad) //oh wait this is the constrctor for Child?

{
...
}
}


public class Child : Parent // make a child class
{
public Child(string mom, string dad)
: base(mom, dad) //oh wait this is the constrctor for Child?

{
...
}
}


x0rld
x0rld2y ago
ehh it's only one class
Aaron
Aaron2y ago
yes that's the constructor for child