I have a small query related to OOP concept in C#.
I have an interface
interface intf
{
string Hello();
}
A base class
public class BaseClass
{
public string Hello()
{
return "Hello of base class called";
}
}
A child class that is derived from BaseClass and implements the interface intf as well
public class ChildClass : BaseClass, intf
{
string Hello()
{
return "Hello of child class called";
}
}
Now my question is that when I create an object of ChildClass then when I call the hello method it always calls the hello method of BaseClass. Firstly, why does it call the Hello of the BaseClass? Secondly, how can I call the Hello of the ChildClass?
private void Form1_Load(object sender, EventArgs e)
{
ChildClass obj = new ChildClass();
MessageBox.Show(obj.Hello());
}
Why call the Hello of base
Your child class implements the interface, so it is considered to contain a public Hello method. But in your child class, Hello is not public. The Hello method in base class is public and is considered to be the implementation of the interface.
How to call Hello of child class
Make the Hello in child class public. Then it will be considered to an implementation of the interface.