Boxing , Unboxing , Virtual,Void ,new keywords And Access Specifiers


Lets get introduced to following terms


  • Boxing- The term boxing stands for converting value types to Object type. Boxing is the ultimate way to treat value type as Object type.The type conversions are happened internally from value type to Object type.

  • In below example the int is the value datatype converted into Object type using Object Data type.

    Example
    int i=7
    Object o=i


  • Unboxing- The term Unboxing stands for converting Object types to value type vice versa of Boxing. It is the opposite of the Boxing. These conversions are also happened implicitly.
  • Example

    Object 0=23
    int i=(int)o



  • VIRTUAL and OVERRIDE Keyword 
  •            virtual keyword is used while implementing method overriding. method overriding stands for overriding method of base class with derived class.


    Program.


    using System;
    namespace virtual_override
    {
        class Base
        {
            public virtual void show()
            {
                Console.WriteLine("Base class method");
                Console.ReadKey();
            }
        }
        class Derived:Base
        {
            public override void show()
            {
         
                Console.WriteLine("derived class method");
                Console.ReadKey();
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Base b = new Base();
                Derived d = new Derived();
       //         b.show();
                d.show();               //will show derived class output
            }
        }
    }


    Output.
    derived class method



  • Using new keyword
  • C# supports method hiding. new keyword is used to explicitly hide member it inherited from base class.

     class Base
        {
            public void show()
            {
                Console.WriteLine("Base class method");
                Console.ReadKey();
            }
        }
        class Derived:Base
        {
            public new  void show()
            {
           //     base.show();
                Console.WriteLine("derived class method");
                Console.ReadKey();
            }
        }


In C sharp , every Object and it's members has been set an accessibility. The term Access  stands for the accessibility level of Objects and members. Access Modifiers provides a level of security to Objects and Classes. We can control which class should be accessed by other classes.


There are total five Access Specifiers in C Sharp.

  • public- the most commonly used access modifier in C Sharp. public access modifier states that there is no restriction and limitation to use Object or Class that this Specifier is assigned to. The public class or Object can be accessed from anywhere , inside or outside of program.


  • private-Opposite to public access modifier , private Object or Classes have very limited scope. They can be accessed only inside the class in which they are declared. 


  • protected- The class or Object to which protected has been assigned can be accessed only with the class and it's derived class.


  • internal-These type of classes and objects can be accessed within  the same program containing it's declaration. But cannot accessed from outside of program.


  • protected internal- Combines accessibility of both protected and internal access specifiers. Can be accessed in same program or class as well as inherited classes.
  • Boxing , Unboxing , Virtual,Void ,new keywords And Access Specifiers Boxing , Unboxing , Virtual,Void ,new keywords And Access Specifiers Reviewed by vishal on July 31, 2017 Rating: 5

    No comments