Difference between Module and Class

Key Difference:  In programming languages, classes are blueprints of objects which contain members like fields and methods. Module introduces the definition related to properties, events, variables and procedures of its members. Modules cannot be instantiated like classes.

In many of the programming languages, classes and modules play a vital role. Classes are often considered as the blueprint of objects. It encapsulates related items. Classes are important as they control the visibility of the members within them, and also define the accessibility. Other important characteristics of classes are that they provide reusability of the code and one can also use inheritance concepts in programming using classes.

Class is considered to be blue print of an object, because it is able to describe all the necessary details associated with an object. An object is created from this class. Class consists of three important things which are – name of the class, attributes and operations.

public class Student

{

    public StudentRegistration ();

    {

        new RecordManager().Initialize();

    }

}

This is an example of class named as Student

A module is also a kind of reference type just like class. Like a class, it also describes the members  within it. However, still they are different from each other.

Module introduces the definition related to properties, events, variables and procedures of its members. This is an example of a module in VB.

Public Module Mymodule

    Sub Main()

        Dim firstname As String = InputBox("What is your firstname?")

        MsgBox("Your first name is" & firstname)

    End Sub

    End Module

A module is also called as a standard module. Module can only be used at namespace level.

One cannot instantiate modules as classes with objects. This is due to the reason that  only one copy of the standard module’s data exists, and in case some changes occur in any part of the program, effecting the public variable of standard variable, then in that case same effect is seen on the other part using this effected variable. On the other hand, data for each object is kept separately. For their sets of benefits modules and classes are generally combined.

Comparison between Module and Class:

Module

Class

Inheritance

Not Supported

supported

Implement interfaces

No

Yes

Members

Shared (implicitly in Visual Basic)

Control the type of a member of a class as a shared or instance member

Object oriented

No, modules cannot be instantiated

Yes

Copy of the data

Only one copy

Object data exists separately for each instantiated object.

Typical application

Generally, used at the outer level for separating compilation, inner structures and privacy outside of the module boundaries

Generally as components of modules

Preferred

For abstract datatypes used with binary operations, such as sets with a merge operation

When inheritance is required

Members

Methods, constants and classes

Methods, constants and variables

Inclusion

Can be included by using include commands in classes or modules

Cannot be included

Super class

Object

Module

ncG1vJloZrCvp2OxqrLFnqmeppOar6bA1p6cp2aZo7Owe8OinZ%2Bdopq7pLGMm5ytr5Wau265zp2spZ1dlrulecKlmKyr