Home > Software design >  Why does base class object cannot able hold child class object
Why does base class object cannot able hold child class object

Time:02-19

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 ?

enter image description here

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, using BaseClass.
  • Here ChildClass is inheriting from BaseClass, that means ChildClass gets all the properties and functions of BaseClass automatically. As there is a relationship between BaseClass and ChildClass, we can define instance of ChildClass using BaseClass type.

why not ChildClass obj2 = new BaseClass(); is not valid ??

  • Because there is no relationship between BaseClass with ChildClass in the reverse order.
  • To define obj2 as of type ChildClass, we need is a relationship between ChildClass and BaseClass.

Let's take a below example

enter image description here

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

  • Related