Question

What is difference between this

DerivedClass classinst = new DerivedClass();

and

BaseClass classinst = new DerivedClass();

I can't seem to see any difference and compiler doesn't complain, so can I use interchangeably?

Was it helpful?

Solution

The only difference between the two is that if you do

DerivedClass classinst = new DerivedClass();

The compiler interprets classinst as a DerivedClass, and allows you to use all the specific methods/properties/etc you have declared in the derived class and not in the base class.

On the other hand, if you do BaseClass classinst = new DerivedClass();

The compiler will interpret classinst as a BaseClass, and you will not have access to those members.

Do realize that the actual type of the object does not change. The reference to the object stays the same, no matter which of the two declarations you use. The only thing that changes is how the reference is interpreted, and therefore what members are available; those of the base class, or the derived class.

OTHER TIPS

In the second example you have no access to the methods or properties of DrivedClass since your variable is defined as BaseClass. Even though an instance of DrivedClass is behind the scenes, you will have no access to members specific to that type.

By using:

BaseClass classinst = new DrivedClass();

You allow yourselft to also do:

BaseClass classinst = new DrivedClassOne();
BaseClass classinst = new DrivedClassTwo();

Which may be nice in a case where lets say you have a road object, which contains a list of cars. You could then add a new Ford or a new Honda to the list because it is of type car, instead of only fords or hondas

The difference is the Type of the "classinst" variable.

Try the following code:

string typename = typeof(classinst).ToString();

If you want to use polymorphic behaviour - ie having a method defined in a base class which behaved differently depending on how it is overridden in the derived class, but not need knowledge of the derived class, then you assign as the base class.

This is typical object oriented behaviour - if you are using subclasses for this purpose, they will almost always be referred to as the base class (or more typically these days as an interface which multiple related classes implement).

Well, with the second version, you won't have access to properties / methods specific to Derived class using your instance (without casting to the derived type).

Imagine

public class Animal {
  public int NbOfEyes {get;set;}
}

public class Dog : Animal {
  public bool IsPolite {get;set;}
}

Animal dog = new Dog();

dog.IsPolite = false; //you can't do that. dog is an animal, it doesn't know it can be polite.
dog.NbOfEyes = 2;//you can do that, you have access to animal's properties /methods

Dog dog2 = new Dog();
dog2.IsPolite = false; // you can do that.
dog2.NbOfEyes = 4; //you can also do that, you have access to base class's properties / methods.

2 of the most important principles in object oriented programming are inheritance and polymorphism.

Inheritance allows to create a class inheriting data and behavior from an existing class. The subclass can then specialize (usually by overriding) the superclass behavior.

Polymorphism allows a subclass to behave as if it were its superclass, so in your case an instance of DerivedClass can be used in any place where an instance of BaseClass is required.

So an instance of DerivedClass can be assigned to a variable of type BaseClass (thanks to polymorphism). But even if assigned to a variable of type BaseClass, it will behave like an instance of DerivedClass, so any overriden behavior will be automatically used.

Example:

class BaseClass
{
    public virtual void ShowMe()
    {
        Console.WriteLine("I'm the base class");
    }
}

class DerivedClass : BaseClass
{
    public override void ShowMe() 
    {
        Console.WriteLine("I'm the derived class");
    }
}

// This will print "I'm the base class"
BaseClass baseClass = new BaseClass();
baseClass.ShowMe(); 

// This will print "I'm the derived class"
DerivedClass derivedClass = new DerivedClass();
derivedClass.ShowMe(); 

// This will print "I'm the derived class"
// Reason: even if assigned to a BaseClass variable,
// it's still a DerivedClass instance
BaseClass baseClass2 = new DerivedClass();
baseClass2.ShowMe(); 

// This will generate a compiler error
// Subclasses can behave like superclasses,
// but the opposite is not possible
DerivedClass derivedClass2 = new BaseClass();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top