Inheritance

As we all know that Inheritance basically for code re-usability. There are some good features which I want to highlight in this article which will help developers to write code in more effective manners and can clear the concept of how we can cast base and derived classes can work with each other methods. Let’s first create the two classes with the help of below codes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class BaseClass
    {
        public int x = 10, y = 20;
        public int z;
        public BaseClass() {
            z = 30;
        }
        public int GetArea() { return (x * y);}
        public int GetAreaNew() { return (x * y); }
        public virtual int GetAreaNewOne() { return (x * y); }
    }

    class DerivedClass : BaseClass
    {
        public DerivedClass() : base() //One can fire the constructor of parent class using base() method.
        {
        
        }
        public int GetArea() {return (2 * x); }
        public new  int GetAreaNew() { return (2 * x); }
        public override int GetAreaNewOne() { return (2 * x); }
    }
}

Now let’s discuss some concept about the inheritance.

Most of the time we just use this OOPS feature to create the base class, defined all the properties and methods which we need in future while creating the other classes and then just inherit the base class and use it’s feature in derived class.

Try:

=> Upcast the object of base with derived will return only base class.

When you up cast the base class object with the derived class then you will not get any error, you only get the access of those properties and method which are there in base class from derived class. In our code example, since we have overridden the code of base class method “GetAreaNewOne()” which is there in base class as virtual so we will get the value return by GetAreaNewOne() method from derived class (Also known as Redefining Members).

 DerivedClass dc = new DerivedClass();
 BaseClass bc = dc;
 int intArea = bc.GetAreaNewOne();

=> Error: can not cast base object to derived object directly.
When you directly cast the object of base class into derived class then you will get the above error.

 BaseClass ih2 = new BaseClass();
 DerivedClass d2 = new DerivedClass();
 DerivedClass d2 = (DerivedClass)ih2; 

But if you have base class object initialized with the derived class object then you will not get that error.

 DerivedClass d = new DerivedClass();
 BaseClass ih = d;
 DerivedClass d2 = new DerivedClass();
 d2 = (DerivedClass)ih;

=> There are two operators available using them we can avoid the exceptions.
Is: which return to true if left side object can be cast to right side object.

   bool bl = d2 is Base;

As: is an alternative way of casting an object.If casting is invalid then it return the null value.

   
   d2 = ih2 as Derived;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s