Programmer's Corner - Object-Oriented Programming (OOP) Tutorial - Example - Class & Interface

Backup and Security Solutions 10% off all products with promo code: VISI-P1YR
Get the Programmer's Corner FireFox Search Plug-In

Class and Interface Example

Before beginning this example make sure you have completed the Introduction to Classes and Introduction to Interfaces tutorials because I will not be giving an in depth explanation of how classes and interfaces work.

This example is just to give you a little more practice using classes and interfaces. The example will use a Bank Account interface that will be used in a Savings Account Class and Checking Account Class. 

Open up Visual Studio .NET and create a new Console Application called Bank.

I will keep this simple so it will just have a Balance property, Debit function, Credit sub, and just because I need an example of a write once property it will have an Account Number. Again changing the Class to Interface.

We will just say AccountNumber has to be greater than zero. Balance is read only because it can only be changed through Debit and Credit. In Debit and Credit the Amount must be greater than zero. In a Savings Account you cannot take out more money than there is in your account. Since debit may not work lets declare it as a function that will return True if it works or False if it doesn't.

Public Interface IBankAccount
    Property AccountNumber() As Integer
    ReadOnly Property Balance() As Double
    Sub Credit(ByVal Amount As Double)
    Function Debit(ByVal Amount As Double) As Boolean
End Interface

AccountNumber will be zero when the object is first created. Write once properties aren't as common anymore since you can just have them supply the value inside the constructor. So when the object is created the AccountNumber is invalid but this isn't going to be used anywhere so it doesn't really matter. If AccountNumber is still zero that means they haven't changed it yet so we will allow them to change it.

Private mintNumber As Integer

Public Property AccountNumber() As Integer Implements IBankAccount.AccountNumber
    Get
        Return mintNumber
    End Get
    Set(ByVal Value As Integer)
        If mintNumber = 0 And Value > 0 Then
            mintNumber = Value
        End If
    End Set
End Property

It's up to you if you want to throw an exception if AccountNumber is invalid or has already been set. Balance will simply return the balance variable and Credit simply adds to the balance as long as the amount is positive.

Private mdblBalance As Double

Public ReadOnly Property Balance() As Double Implements IBankAccount.Balance
    Get
        Return mdblBalance
    End Get
End Property

Public Sub Credit(ByVal Amount As Double) Implements IBankAccount.Credit
    If Amount > 0 Then
        mdblBalance += Amount
    End If
End Sub

The Credit function works a little different. As I said in a savings account you cannot take out more money then there is in your account so we must determine if they can take out the money and also return whether the debit went through.

Public Function Debit(ByVal Amount As Double) As Boolean Implements IBankAccount.Debit
    If Amount > mdblBalance Then
        Return False
    Else
        mdblBalance -= Amount
        Return True
    End If
End Function

I would also like to add two constructors: one with no parameters and the other with one parameter which will be the starting balance of the account. The first constructor will simply set the balance to zero and the second will set the balance to the number they supply as long as it's not negative.

#Region "Constructor(s)"
    Public Sub New()
        mdblBalance = 0
    End Sub

    Public Sub New(ByVal StartingBalance As Double)
        If StartingBalance >= 0 Then
            mdblBalance = StartingBalance
        Else
            Throw New ArgumentException("Balance cannot be negative")
        End If
    End Sub
#End Region

Q: Why don't we just do Me.Balance = StartingBalance like we did in interface tutorial for the shapes? A: Balance is read only and therefore has no Set so you can't use it to validate the StartingBalance. That means you must do it yourself inside the constructor. I decided to throw an error if StartingBalance is invalid.

Now lets do the CheckingAccount class. This class is almost exactly like the SavingsAccount class but there is no need to check the balance because it can be negative. Add a new class and call it CheckingAccount. You can simply copy the code from SavingsAccount and make the slight modifications to it.

Public Class CheckingAccount
    Implements IBankAccount

    Private mdblBalance As Double
    Private mintNumber As Integer

#Region "Constructor(s)"
    Public Sub New()
        mdblBalance = 0
    End Sub

    Public Sub New(ByVal StartingBalance As Double)
        mdblBalance = StartingBalance
    End Sub
#End Region

    Public Property AccountNumber() As Integer Implements IBankAccount.AccountNumber
        Get
            Return mintNumber
        End Get
        Set(ByVal Value As Integer)
            If mintNumber = 0 And Value > 0 Then
                mintNumber = Value
            End If
        End Set
    End Property

    Public ReadOnly Property Balance() As Double Implements IBankAccount.Balance
        Get
            Return mdblBalance
        End Get
    End Property

    Public Sub Credit(ByVal Amount As Double) Implements IBankAccount.Credit
        If Amount > 0 Then
            mdblBalance += Amount
        End If
    End Sub

    Public Function Debit(ByVal Amount As Double) As Boolean _
            Implements IBankAccount.Debit
        mdblBalance -= Amount
        Return True
    End Function
End Class

You can now continue to the Class & Interface Example - Executable where I will make a simple menu driven console application that uses these to classes.

Author Information:

Adam Schentag

http://www.programmers-corner.com

adam@programmers-corner.com

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

There are currently no comments available.