Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded methods - references and instantiated objects difference

In the code snippet above i have a base class Shape and two derived classes from it Rectangle and Triangle. I instantiate them but for the one Triangle object I use reference type of his base class.
So now when I call a method calculate() it will prefer to call the method that is taking the base class argument.

What is the purpose of this?

I am creating Triangle object not Shape object I just use the reference of the base class. And my other question is are they any other differences from using the reference of the base class and instantiating the object from the derived instead of using derived reference?

public static void Main(string[] args)
{
    Point tc = new Point(3, 4);
    Point rc = new Point(7, 5);

    Shape shape = new Triangle(tc, 4, 4, 4);

    calculate(shape);
}

public static void calculate(Shape shape) <<-- new Triangle()  with base class ref will came here.
{
    Console.WriteLine("shape");
}

public static void calculate(Rectangle rectangle) 
{
    Console.WriteLine("rectangle");
}

public static void calculate(Triangle triangle) <<-- new Triangle() using triangle ref will came here.
{
    Console.WriteLine("triangle");
}

1 Answers

1ST Question: So now when I call a method calculate() it will prefer to call the method that is taking the base class argument. What is the purpose of this?

Answer: How could it be any other way? The compiler would have no way to determine the "actual" type of the object because that can only be truly known at run time. It would be your responsibility to use "reflection" (Such as GetType() and typeof() ) to cast that parent object (shape) into its child (triangle) and then pass that as a argument. If it worked any other way, you could not properly implement the method below.

 public foo(object var1){
     // Test for var1's type and perform a operation
 }

2nd Question: Is are they any other differences from using the reference of the base class and instantiating the object from the derived instead of using derived reference?

Answer: In memory no, the reference is always pointing to the same data but the Context will change. This means that which type of object(child or parent) the object is determines which fields/methods can be accessed. Which type the object is cast to does not alter what is actually stored on the heap, rather it changes what you can access. This is demonstrated from the below snippet of code.

    public class Parent {
        public int var1 = 1;
    }

    public class Child : Parent {
        public int var2 = 2;
    }

    public void foo () {
        Parent theObject = new Child();
        int num1 = theObject.var1;          // Valid
        int num2 = theObject.var2;          // Invalid
        int num3 = ((Child)theObject).var2; // Valid
    }
like image 79
Cabbage Champion Avatar answered Feb 15 '26 13:02

Cabbage Champion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!