Thursday, October 26, 2017

What is upcasting and downcasting in c sharp

What is upcasting in c sharp

upcasting means assign the derived class object to Super class
example :
  class A                                                                    // create the class A it is a Super classs
       {
         public void show()                                          // create the method inside class B
              {
              Console.WriteLine("Show method")
              }
        }
  class B :A                                                                 // create a class B and inherited the Class A
        {
         public void display()                                         // create the method display
              {
              Console.WriteLine("display method")
              }
         }
 class main
    {
    static void main()
    {
        A obj =new B();                //"upcasting"  here we assigned as derived class object to Super class
        obj.show();                                     //it is working in run time
        obj.display();                                     //not working in run time so in run time the derived class                                                                              methods not working
                                                                   
     }
     }

Steps to create upcasting

so in the above example we created 3 classes. one class for main method remaining two classes for inheritance concept.
 class A has one method,
 class B has one method,
 now i created a object for class B and assigned to class A reference type in main class

A obj =new B();   the statement is a upcasting


Note :

in upcasting "super class reference, can access the super class method and override methods in derived class".

with super class reference we can access override methods in derived class.
super class can hold the any type of objects of all derived class.


Example for Override concept :-
         
 using System;
namespace upcasting
{
    class A
    {
     
        public virtual void rect()
        {
            Console.WriteLine("super class rect");
        }

    }

    // Derived class
    class B : A
    {
        public override void rect()
        {
            Console.WriteLine("derived class override");
        }
    }

    class rectangle
    {

        static void Main(string[] args)
        {
            A obj = new B();
            obj.rect();
            Console.ReadKey();
        }
    }
}
the out put of above program is
 "derived class override"


  • in the above program "the override method in derived class is executed with super class reference".
  • how to execute the derived class methods with super class reference(without override methods)
  • if you want execute the derived class methods with super class reference then you have to use the typecasting. put the typecasting for super class with derived class.
  • if you want to execute the methods in derived class then you have to use the downcasting.


example for downcasting


using System;
namespace upcasting
{
    class A
    {
        public  void width()
        {
            Console.WriteLine("super class width");
        }

        public void height()
        {
            Console.WriteLine("super class height");
        }
        public virtual void rect()
        {
            Console.WriteLine("super class rect");
        }

    }

    // Derived class
    class B : A
    {

        public  void area()
        {
            Console.WriteLine(" derived class ares");
        }
        public override void rect()
        {
            Console.WriteLine("derived class override");
        }
    }

    class rectangle
    {

        static void Main(string[] args)
        {

            A obj = new B();
            B obj1 = (B)obj;
            obj1.area();
            Console.ReadKey();
        }
    }
}

out  put:- derived class override

Rule for downcasting
  1) upcasting is mandatory
  2) typecasting mandatory



No comments: