Saturday 24 August 2013

Inheritance C#

Leave a Comment
Inheritance is one of the main features of an object-oriented language. C# and the .NET class library are heavily based on inheritance. The telltale sign of this is that all .NET common library classes are derived from the object class, discussed at the beginning of this article. As pointed out, C# doesn’t support multiple Inheritance. C# only supports single inheritance; therefore, all objects are implicitly derived from the object class.

Implementing inheritance in C# is similar to implementing it in C++. You use a colon (:) in the definition of a class to derive it from another class.

In this example. i will create 3 class which are Class BaseClassA, BaseClassB, and Main Class which is myClass

Inheritance example

    // Base class A
    class BaseClassA
    {
          publicvoid MethodA()
          {
              Console.WriteLine("A Method called");
          }
    }


    // Base class B is derived from Base class A
    class BaseClassB : BaseClassA
    {
          public void MethodB()
          {
            Console.WriteLine("B method called");
          }
    }

    // Main class
    class myClass
    {
       static void Main()
       {
           // Base class B
           BaseClassB b = newBaseClassB();
           // Base class B method
           b.MethodB();
           // BaseClassA Method through BaseClassB
           b.MethodA();
          }
    }

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.