0
 class TestA{

some code.....

}

class TestB{
.....
}


class Program{
void Main(){
TestA obj= new TestB();////When and why do we sometimes do this?
}
}

What are the different scenarios when we would have to refer one object to another class?

Nish
  • 183
  • 7
  • 2
    This is the not the scenario explanation site.. please see [Polymorphism (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism) and when you have a problem come back and see us – TheGeneral Aug 03 '18 at 03:28
  • 1
    You're referring to Polymorphism and Inheritance. Please review this question and research from there. https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism – Damon Earl Aug 03 '18 at 03:30

4 Answers4

2

We don't. We created a variable called obj, and declared the variable to be of type TestA. That means that that variable can contain a reference to any object this IS-A TestA.

You then create a TestB object. Presumably, TestB derives from TestA, which is not shown in your question. But that means that this new object, is, generally, a TestA, as well as being, specifically, a TestB. We then assign a reference to this object to the obj variable.

Which is fine. It still is a TestB object. It's just that this code, clearly, doesn't intend to use any of it's B-ish nature. Just the core A-ish part that it shares; It's also possible that the TestB class overrides some of TestA's members, in which case it will still demonstrate it's B-ish nature when those members are accessed.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

From your code example this approach could be used if TestB inherits from TestA. If you're unsure what inheritance is you should read a bit about Object Oriented programming. Another approach where you would have a class which creates other objects is if you are using a Factory Pattern. There's plenty of information on the web about this pattern too. If you are using a factory pattern you wouldn't use the same constructor approach as in your code though (i.e. you wouldn't expect a new instance of an object to return a different object.)

Greg the Incredulous
  • 1,676
  • 4
  • 29
  • 42
0

We can only do this when class have parent-child relationship,Otherwise it can't be possible to assign one class memory to another class.

Read More...1

Read More...2

Hiren Patel
  • 1,071
  • 11
  • 34
0

the answer to this as much as i know, this could be in two cases: 1-Polymorphism. 2-Interfaces.

I'll show u how:

Polymorphism is like :

//an example of Polymorphism.
class FamilyMembers //parent class
{
    public virtual void GetData() //it's virtual method cuz it can be overridden later
    {
        Console.WriteLine("Family");
    }
}
class MyBrother : FamilyMembers //child class
{
    public override void GetData() //the same method that we wrote before has been overridden
    {
        Console.WriteLine("Bro");
    }
}
class Program
{
    static void Main(string[] args)
    {
        //here's what u asking about
        FamilyMembers myBrother = new MyBrother(); //MyBrother is a family member, the system now will choose the GetData() method from the child class MyBrother
        myBrother.GetData();
        Console.ReadLine();
    }
}

Interface is like:

public interface IFamily //the Parent Class
{
    //an interface holds the signature of it's child properties and methods but don't set values

    //Some properties signatures
    int Age { get; set; }
    string Name { get; set; }
    //some methods
    void PrintData();
}

public class MyBrother : IFamily //Child class that inherits from the parent class
{
    //some properties, methods, fields

    public string Name { get; set; } //public required
    public int Age { get; set; } //public required
    private string Collage { get; set; } //for my brother only 
    //constractor that sets the default values when u create the class
    public MyBrother()
    {
        Name = "Cody";
        Age = 20;
        Collage = "Faculty of engineering";
    }
    ////a method
    void IFamily.PrintData()
    {
        Console.WriteLine("Your name is: " + Name + " and your age is: " + Age + " and you collage is: " + Collage);
    }
}


class Program
{
    static void Main(string[] args)
    {
        //now let's try to call the the methods and spawn the child classes :)

        //spawn the child class (MyBrother) that inherits from the Family interface
        //this is the answer of ur question
        IFamily myBrother = new MyBrother(); // the constructor will auto-set the data for me so i don't need to set them
        //printing the dude
        myBrother.PrintData();
        Console.ReadLine();  
    }
}

I hope this will do :)