Senthur
Senthur

Reputation: 333

Difference between instantiating using base class and derived class

Can someone explain me the difference between instantiating a derived class by referring through the base class and instantiating a derived class by the derived class name itself.

Please go through this program

    using System;

    abstract class Test
    {
        public int _a;
        public abstract void A();
    }

    class Example1 : Test
    {
        public override void A()
        {
        Console.WriteLine("Example1.A");
        base._a++;
        }
    }

    class Example2 : Test
    {
        public override void A()
        {
        Console.WriteLine("Example2.A");
                base._a--;
        }
    }

    class Program
    {
        static void Main()
        {
        // Reference Example1 through Test type.
        Test test1 = new Example1();
        test1.A();

        Example2 test2 = new Example2();
        test2.A();
        }
    }

Here is the question again, inside the Main() method two objects are created.

First object test1 is referenced through Test(base class) type, what does it actually mean ? and what difference does it make comapred to the second object which is based on the derived class ?

In a program which is preferred, referencing the base class or the derived class, how does it help ?

I know my question is a little vague but any idea on this would be very helpful for me to learn further. Thanks in advance.

Upvotes: 0

Views: 1734

Answers (2)

Li-Fan Yu
Li-Fan Yu

Reputation: 315

Test test1 = new Example1();

When you new a derived class object using base class type, you're using the concept of polymorphism.

In your example, we can't see the advantage of using polymorphism, but applying polymorphism helps us to write succinct codes in some other cases. For example, when you need many objects of different derived classes to call their virtual(or abstract) methods, you can make a list of the base class but new each object as different derived class so that you can just traversed the list and call the method in a succinct way.

It may be hard to understand it in words, but you can find a general using of polymorphism in the first example code of this page, where we can compress the code of calling many virual methods of different derived classes to only 1 line(s.Draw();). So without applying polymorphism, we can only new many objects separately and call the method Draw() separately, which is obviously not a good way to code in such case.

Upvotes: 1

Gent
Gent

Reputation: 2685

When approaching the use of inheritance and for that matter abstraction in general, I would consider what do you need to use from the class, and then use the most general (abstract) version of its type representation to hold in variables and method signatures (e.g. Example1).

Test test1 = new Example1();
test1.A();

As long as you are not violating SOLID principles, what this will buy you is freedom to refactor with the least resistance, ensuring that you don't at a glance think that the code is dependent on any ancillary methods that are specific to the derived class.

Example2 gives the impression that the code is dependent on exactly that implementation, this can be appropriate, but in general indicates tight coupling.

Example2 test2 = new Example2();
test2.A();

This is a difference at Design/Compile time not at run-time. At run-time they both yield the same results, with the only exception being when using reflection. What happens on the right side of the equals is unaffected by the type definition on the left side of the equals.

Upvotes: 0

Related Questions