How can i hide the base class method in derived class?

Showing Answers 1 - 4 of 4 Answers

swati saxena

  • Aug 28th, 2006
 

by adding the new keyword.the member hides the inherited member with the same signature

  Was this answer useful?  Yes

preethi

  • Aug 28th, 2006
 

use "sealed"

  Was this answer useful?  Yes

satya rapelly

  • Oct 5th, 2006
 

Yes -

using System;

 

public class Parent

{

    string parentString;

    public Parent()

    {

        Console.WriteLine("Parent Constructor.");

    }

    public Parent(string myString)

    {

        parentString = myString;

        Console.WriteLine(parentString);

    }

    public void print()

    {

        Console.WriteLine("I'm a Parent Class.");

    }

}

 

public class Child : Parent

{

    public Child() : base("From Derived")

    {

        Console.WriteLine("Child Constructor.");

    }

    public new void print()

    {

        base.print();

        Console.WriteLine("I'm a Child Class.");

    }

    public static void Main()

    {

        Child child = new Child();

        child.print();

        ((Parent)child).print();

    }

}

The Child print() method hides the Parent print() method.  The effect is the Parent print() method will not be called, unless we do something special to make sure it is called.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions