Inheritance in C#

Hello Friends

How can I inherit class usign Access Modifier (public, protected, private, internal) in C#.

for e.g. in C++ we can derive as follow

class DeriveClass : public BaseClass{ }

or

class DeriveClass : private BaseClass{ }

or

class DeriveClass : protected BaseClass{ }

how can i do the same in C#.



Answer this question

Inheritance in C#

  • JonathanTew

    try the below code.

    using System;
    namespace mainN
    {
    class A
    {
    public void methodA()
    {
    Console.WriteLine("A");
    }
    }

    class B : A
    {
    public void methodB()
    {
    methodA();
    Console.WriteLine("B");
    }
    }
    class mainClass
    {
    static void Main()
    {
    B b = new B();
    b.methodB();
    }
    }
    }




  • TOM PHAM

    In fact, the only justification for private inheritence I've ever heard is to override pure virtual methods.

  • bubberz

    C# doesn't support protected or private inheritence, but most of the time when protected or private inheritence is used, what is really needed is protected or private containment.
  • gebhartj

    You can't, the CLI only supports public inheritance.



  • Inheritance in C#