when i try to create new child class object and assign it to a base class object ,During compile time it cannot able to find method of child class... , yes base class is not known to all child class methods.
But why base class is able to hold only child class variables with data and why not methods ?
public class baseclass
{
public string name = string.Empty;
public void member()
{
Console.WriteLine("base");
}
}
public class childclass : baseclass
{
public string game = string.Empty;
public new void member()
{
Console.WriteLine("child");
}
public void testingmethod()
{ Console.WriteLine("child 2"); }
}
class Program
{
static void Main(string[] args)
{
baseclass baseObj = new childclass() { name = "test", game = "test" };
baseObj.member();
baseObj.testingmethod();
baseObj.member();
string str = "abcd";
Console.WriteLine(str);
char jat = Convert.ToChar(str.ToCharArray()[0]);
Console.WriteLine("Hello World!");
//ITest obj = new TestClass();
//obj.TestMethod(); //Way to call implicitely implemented method
//ITest obj2 = new TestClass1();
//obj2.TestMethod();
}
}
As per my understanding for below question new object of childclass
is stored inside baseclass
. but why base class obj can able to hold child class object?
The child class is known to base as well as child class . then why child class not able to hold object of base class?
What is the exact meaning of BaseClass obj = new ChildClass();?
why not ChildClass obj2 = new BaseClass(); is not valid ??
CodePudding user response:
i just copied your code and everything went fine
but in case of rewriting a function in a child object, you will have to change few things in order to be able to call them first of all in the base class:
public virtual void member()
{
Console.WriteLine("base");
}
you need the function to set as virtual function in order to change it in the child instance and in the child instance:
public override void member()
{
Console.WriteLine("child");
}
you will need to override the function also it seems like you try to create an array after that or something like that, if you will want to send parameters you will have to change it in the function and then send it in the constructor. the create function suppose to look like that:
baseclass baseObj = new childobj();
and then the code suppose to work perfectly!
CodePudding user response:
This functionality is called Inheritance
. One of the four pillar of Object-oriented programming language.
What is the exact meaning of BaseClass obj = new ChildClass();
?
- It means you are creating an instance of
ChildClass
, usingBaseClass
. - Here
ChildClass
is inheriting fromBaseClass
, that meansChildClass
gets all the properties and functions ofBaseClass
automatically. As there is a relationship betweenBaseClass
andChildClass
, we can define instance ofChildClass
usingBaseClass
type.
why not ChildClass obj2 = new BaseClass(); is not valid ??
- Because there is no relationship between
BaseClass
withChildClass
in the reverse order. - To define obj2 as of type
ChildClass
, we need is a relationship betweenChildClass
andBaseClass
.
Let's take a below example
Here,
A Dog is an Animal, Correct?
An Animal is a Dog ????? -> No right, Animal can be Tigar, Lion, Elephant or Dog!
Image source: Random Google Search. All credit goes to the image owner