Home > Back-end >  How to implement a class when it is defined in an interface?
How to implement a class when it is defined in an interface?

Time:12-31

An interface can have a class in it but I do not understand how to implement such an interface. I give an example below:

I wrote the following simple code without any interface:

Imports System

Module Program
    Sub Main()
        Dim n As New name("Mounisha", "Ghosh")
        Dim student As New Student(n, 14)
        Console.WriteLine("First Name: {0}", student.studentName.firstName)
        Console.WriteLine("Last Name: {0}", student.studentName.lastName)
        Console.WriteLine("Age: {0}", student.age)
        Console.ReadLine()
    End Sub
End Module

Class name
    Public Property firstName As String
    Public Property lastName As String

    Sub New(ByVal f As String, ByVal l As String)
        firstName = f
        lastName = l
    End Sub
End Class

Class Student
    Private nameValue As name
    Public ReadOnly Property studentName() As name
        Get
            Return nameValue
        End Get        
    End Property
    Public Property age As Integer
    Sub New(ByVal n As name, ByVal a As Integer)
        nameValue = n
        age = a
    End Sub
End Class

Then I tried to re-organize by putting the class name in an interface and implement it in the class student. But I was not able to construct my code. I give the overall idea of the code I am trying to construct below:

Imports System

Interface IClass
    Class name
        Public Property firstName As String
        Public Property lastName As String

        Sub New(ByVal f As String, ByVal l As String)
            firstName = f
            lastName = l
        End Sub

    End Class
End Interface

Module Program
    Sub Main()
        'How to create instances and pass values to constructors?
    End Sub
End Module

Class Student : Implements IClass
    'The idea is to create a readonly property of type "name"
    'But not able to use the Interface to do the same
    Public Property age As Integer
End Class

The main idea is to understand how to implement an interface with a class in it. Pls help.

The other point I want to highlight is that in the constructor of the class name I have written End Sub. Now we cannot use the end sub statement in interface - so why am I not getting an error? Also do the statements firstName = f and lastName = l not denote an implementation which should have been flagged as an error by the compiler - but not getting any error. Any explanations for this?

CodePudding user response:

What you're doing is simply adding a nested class to the interface. This isn't seen as something that the implementing class has to implement but instead is just a class available via that interface. Using your structure, in order to require Student to implement a read only name property, you have to add that property to the interface.

Public Interface IClass
    ReadOnly Property Fullname As Name

    Class Name
        Public Property FirstName As String
        Public Property LastName As String

        Sub New(firstName As String, lastName As String)
            Me.FirstName = firstName
            Me.LastName = lastName
        End Sub
    End Class
End Interface

Your Student Class would then look like

Public Class Student : Implements IClass
    'Because Name is nested within IClass, it's referenced by IClass.Name
    Public ReadOnly Property Fullname As IClass.Name Implements IClass.Fullname
    Public Property Age As Integer

    Public Sub New(name As IClass.Name, age As Integer)
        Fullname = name
        Me.Age = age
    End Sub
End Class

And creating a student would be

Dim name = New IClass.Name("Mounisha", "Ghosh") 'again we access name via the interface
Dim student = New Student(name, 14)

Usually you would not add a nested Class to the interface but instead do something like

Module Program
    Sub Main()
        Dim studuent = New Student(New Name("Mounisha", "Ghosh"), 14)
    End Sub
End Module

Interface IClass
    ReadOnly Property Fullname As Name
End Interface

Public Class Name
    Public Property FirstName As String
    Public Property LastName As String

    Sub New(firstName As String, lastName As String)
        Me.FirstName = firstName
        Me.LastName = lastName
    End Sub
End Class

Class Student : Implements IClass
    Public ReadOnly Property Fullname As Name Implements IClass.Fullname
    Public Property Age As Integer

    Public Sub New(name As Name, age As Integer)
        Fullname = name
        Me.Age = age
    End Sub
End Class
  • Related