Abstract Class with simple example in C# and VB.Net

 
An abstract class contains abstract members which define what a subclass should contain. These abstract members only declare that a member of a particular type is required; it does not implement the member. Implementation of abstract members takes place within the derived class.

In Vb.net we use MustInherit in place of abstract in C#.

Example

[Vb.Net ]

  1. Public MustInherit Class AbstractClass
  2.   Sub New()
  3.  
  4.   End Sub
  5.   Public MustOverride Sub Hello()
  6.   Public MustOverride Function GetDouble(ByVal number As Integer) As Long
  7.   Public MustOverride Function getHalf(ByVal number As Integer) As Long
  8. End Class
  9.  
  10. Public Class drivedClass
  11.    Inherits AbstractClass
  12.    Public Overrides Sub Hello()
  13.      ' Hello Message
  14.        MessageBox.Show("Hello")
  15.    End Sub
  16.    Public Overrides Function GetDouble(ByVal number As Integer) As Long
  17.       ' Get Double of any Number
  18.        GetDouble = 2 * number
  19.    End Function
  20.    Public Overrides Function getHalf(ByVal number As Integer) As Long
  21.       ' Get Half of any number
  22.        getHalf = number / 2
  23.    End Function
  24. End Class

[C#]

  1. public abstract class AbstractClass
  2. {
  3.    public AbstractClass()
  4.    {
  5.    }
  6.    public abstract void Hello();
  7.    public abstract long GetDouble(int number);
  8.    public abstract long getHalf(int number);
  9. }
  10.  
  11. public class drivedClass : AbstractClass
  12. {
  13.     public override void Hello()
  14.       {
  15.           // Hello Message
  16.           MessageBox.Show("Hello");
  17.        }
  18.  
  19.    public override long GetDouble(int number)
  20.        {
  21.            // Get Double of any Number
  22.             return 2 * number;
  23.         }
  24.     public override long getHalf(int number)
  25.         {
  26.             // Get Half of any number
  27.             return number / 2;
  28.          }
  29. }

 

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>