Abstract Classes (Virtual Class)

Unknown | 12:40 AM |

So far, we have seen how to inherit the class, how to overload and override
methods. In the examples shown in inheritance topic, parent class has been
useful in both inheritance and create an instance also.
Actually in some situation the classes are required only for inheritance, such type
of classes are called Abstract classes. This concept is very useful when
creating a framework for the applications where there will not be any
implementation for the methods and properties in the abstract class. It just
defines the structure.
Abstract classes are declared using MustInherit and MustOverride keyword.
Syntax:-
Public MustInherit Class AbstractBaseClass
Public MustOverride Sub DoSomething()
Public MustOverride Sub DoOtherStuff()
End Class
public abstract class AbstractBaseClass
{
public abstract void DoSomething();
public abstract void DoOtherStuff();
}
MustInherit keyword is used with the class name, where as MustOverride
keyword is used with the members of the class.
Implementaion:-

Public Class DerivedClass
Inherits AbstractBaseClass
Public Overrides Sub DoSomething()
MsgBox("This method is overrides the Base class DoSomething to implement the
method functionality”)
End Sub
Public Overrides Sub DoOtherStuff()
MsgBox(“This method is overrides the Base class DoOtherStuff to implement the
method functionality”)
End Sub
End Class


public class DerivedClass : AbstractBaseClass
{
public Override void DoSomething()
{
MessagegBox.Show("This method is overrides the Base class DoSomething to
implement the method functionality”);
}
public Override void DoOtherStuff()
{
MessaggeBox.Show(“This method is overrides the Base class DoOtherStuff to
implement the method functionality”);
}
}
MustInherit forces the classes to be inherited from the derived class and write
an implementation code for the methods and properties in the derived class
before using it.
As in the above example, any class that inherits AbstractBaseClass must
implement the Dosomething and DoOtherStuff methods.
We cannot create an instance of the AbstractBaseClass as shown below.
Dim obj as New AbstractBaseClass()
‘Error in Decleration
AbstractBaseClass obj = new AbstractBaseClass()
‘Error in Decleration
Restricting Inheritence
If we want to prevent a class being used as a Base class we can use
NotInheritable keyword with the class declaration.
Public NotInheritable Class NormalClass
'Decleration of Class members
End Class
public sealed class NormalClass
{
'Decleration of Class members
}
For example when we want an employee class need not to be used as a Base
class, we can declare the employee class structure with NotInheritable keyword.
So that it can be used only for instantiation.

VB.NET
Class NotInheritable Employee
Inherits Person
Public EmpId As Integer
Private Sal As Double = 0
Public Basic As Double
Public Allowance As Double
Public Deductions As Double
Public Shadows Sub DisplayInfo()
Dim msg As String
msg = MyBase.DisplayInfo("")
msg = msg & "ID : " & EmpId.ToString & vbCrLf
msg = msg & "Basic : " & Basic.ToString & vbCrLf
msg = msg & "Allowances : " & Allowance.ToString & vbCrLf
msg = msg & "Deductions : " & Deductions.ToString & vbCrLf
msg = msg & "Net Salary : " & Basic.ToString & vbCrLf
MsgBox(msg)
End Sub
Public ReadOnly Property Salary() As Double
Get
Return Sal
End Get
End Property
Public Sub ProcessSalary()
Sal = Basic + Allowance - Deductions
End Sub
End Class
Following decleration is not posssible, .NET generates error!!!!
Public Class Staff
Inherits Employee
End Class

C#

class sealed Employee: Person
{
public int EmpId ;
private double Sal = 0;
public double Basic;
public double Allowance;
public double Deductions;
public new void DisplayInfo
{
string msg;
msg = Base.DisplayInfo("");
msg = msg + "ID : " + EmpId.ToString;
msg = msg + "Basic : " + Basic.ToString;
msg = msg + "Allowances : " + Allowance.ToString;
msg = msg + "Deductions : " + Deductions.ToString;
msg = msg + "Net Salary : " + Basic.ToString;
MessageBox.Show(msg);
}
public double Salary
{
get
{
return Sal;
}
}
public void ProcessSalary()
{
Sal = Basic + Allowance – Deductions;
}
}
Following decleration is not posssible, .NET generates error!!!!
Public Class Staff: Employee
{
----
}

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....