Having a base class function depend on its child class C#
Posted
by
Junk Junk
on Stack Overflow
See other posts from Stack Overflow
or by Junk Junk
Published on 2012-09-30T13:24:30Z
Indexed on
2012/09/30
15:37 UTC
Read the original article
Hit count: 259
I have a Base class with a method that a child class will almost always override. However, instead of replacing the base class' method entirely, I would like for whatever is derived in the child class to be added to what is already in the base class.
For Example:
class BaseClass
public string str()
{
var string = "Hello my name is" ;
}
class ChildClass : BaseClass
public override string str(){
var string = "Sam";
}
The point is that if I want to access the str() method by creating an instance of the ChildClass, the string will print out as "Hello, my name is Sam".
I've been looking around and all I have been finding is that this should NOT happen, as the base class shouldn't even know that it is being inherited. So, if the design is false, how would I go about doing this? Keep in mind that there will be multiple child classes inheriting from BaseClass.
Thank you
© Stack Overflow or respective owner