Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to perform method hiding in ASP.NET C#

    • 0
    • 1
    • 1
    • 5
    • 0
    • 0
    • 0
    • 0
    • 177
    Comment on it

    Hello all,

    To understand the concept of method hiding C# lets consider an example where we have class (User) that have some properties and methods.

    Ex.

    public class User
    {
       public string fisrtName { get; set; }
       public string lastName { get; set; }
    
       public void PrintFullName()
       {
           Console.WriteLine("Full name is - " + fisrtName + " " + lastName);
       }
    }
    

    Now we create another class with name AdminUser which inherits from User class :

    public class AdminUser : User
    {
        public void PrintFullName()
        {
            Console.WriteLine("Full name is - " + fisrtName + " " + lastName);
        }
    }
    

    Now here we see, just as we crate a function with the same name (PrintFullName()) in side our derived class that is AdminUser we get the warning message like this :

    alt text

    So, to get rid of this situation and hide the method of base class intentionally, we can user new keyword before creating the method in child class like this :

    public new void PrintFullName()
    {
       Console.WriteLine("Full name is - " + fisrtName + " " + lastName + " - Admin User");
    }
    

    Now while calling the PrintFullName() method of AdminUser class, if in any case we want to call the method of base class we can do that in various ways, such as :

    Ex 1:

    static void Main(string[] args)
    {
       User UserObj = new AdminUser();
       UserObj.fisrtName = "First Name";
       UserObj.lastName = "Last Name";
       UserObj.PrintFullName();
    }
    

    Here we are creating the parent class reference variable, and as the child class can have all the properties of parent class, so this allows us to assign an object of child type to the reference variable of parent class. but vise versa it is not possible.

    Ex 2 : we can also use the base keyword, as base keyword points to the base class, so this allows us to call base hidden method of the base class.

    public new void PrintFullName()
    {
       base.PrintFullName();
    }
    

    Ex 3 : we can also perform type casting of the child class object to the base class to call the hidden methods of the base class.

    static void Main(string[] args)
    {
       AdminUser adminUserObj = new AdminUser();
       adminUserObj.fisrtName = "First Name";
       adminUserObj.lastName = "Last Name";
       ((User)adminUserObj).PrintFullName();
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: