Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide method in .Net Hierarchy

Tags:

c#

inheritance

I have 3 classes:

  1. BaseClass
  2. Middleclass inheriting from BaseClass
  3. ClientClass inheriting the Middleclass

I wonder how do I hide a method at BaseClass to not appear in ClientClass?

Example:

public class BaseClass
{
    public void BaseMethod1()
    {
    }

    public void BaseMethod2()
    {
    }
}

public class MiddleClass : BaseClass
{
    public void MiddleMethod()
    {
        this.BaseMethod1();
    }
}

public class ClientClass : MiddleClass
{
    public void Test()
    {
        this.MiddleMethod();
        this.BaseMethod1(); // I can't see this method here
    }
}

Edit: I modified my sample, I put "this.BaseMethod1();" in MiddleClasse

like image 626
Nandoviski Avatar asked Sep 14 '26 17:09

Nandoviski


1 Answers

You need to define that method as private. Just for reference, here you have more information about access modifiers

public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private: The type or member can be accessed only by code in the same class or struct.

protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

like image 144
Claudio Redi Avatar answered Sep 17 '26 05:09

Claudio Redi