You must Sign In to post a response.
  • Category: .NET

    How to hide a method in derived class

    Hi Everyone,

    I have a class that have some method which should not be accessed in derived class.How do I handle this.If we want to hide class itself we can make it as sealed.But what about method prevention alone.?

    Thanks in advance,
    Sharmila
  • #767848
    Hi

    You can make methods private in the parent class, so that those parent class methods are not accessible in child class even if we create object for child class or parent class since we are making method private.
    Refer below code snippet.

    public class parent
    {
    private void ParentMethod()
    {
    //some logic
    }
    public void ParentMethod2()
    {
    //some logic
    }
    }

    public class child : parent
    {

    }

    public static Void Main()
    {
    parent _parentobject=new parent();
    _parentobject.ParentMethod(); //this method cannot be accessed since private method in parent class.
    _parentobject.ParentMethod2(); //this method can be accessed since public method in parent class.

    child _childobject=new child();
    _childobject.ParentMethod(); //this method cannot be accessed since private method in parent class.
    _childobject.ParentMethod2(); //this method can be accessed since public method in parent class.

    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #767866
    Hi,

    If you want to use the method with in class then declare the method type as Private using this you can hide the methods in derived classes.

    declare the method like this in your parent class

    private void methodname()
    {
    }


    now in your derived class you will not get this method for inheriting.

    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767898
    hi,

    Refer below:
    Private:
    Only members of the class can access the varibales

    Public:
    All the members can access that variable

    Protected:
    If any variable is declared as protected, so tat class and its derived class can only access.

    Friend:
    Only members in current project have access to the elements.

    Ex:

    class Sample
    {
    private void a();
    }
    class sampleb
    {

    }
    a is only accessible in Sample.

    If a is public void a();

    a is accessible in Sample and sampleb.


    class Sample
    {
    Protected void a();
    }
    class sampleb : sample
    {

    }

    a is accessible in Sample and sampleb.


    Hope this will give you an idea

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #767909
    Hai Sharmila,
    If you want to hide the class in the derived class, you can make the class as Sealed. The method can be made as private. So the private method can't be accessed to the derived class.
    Private class members are inherited but cant be accessible.

    class MyClass
    {
    private void MyMethod()
    {
    // do something
    }
    }
    class MyChild : MyClass
    {
    // method will not be accessible here.
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments