# Class
{: .no_toc }

Defines a class. Classes are templates from which objects are created --- classes are object types, as opposed to value types. Objects are held by reference and are reference-counted. The memory an object occupies is freed when there are no more references to it --- when no variables in the process refer to them.

Syntax:

> [ *attributes* ]  
> [ **Public** \| **Private** ] **Class** *name* [ **(** **Of** *typevars* **)** ]  
> &nbsp;&nbsp;&nbsp;&nbsp;[ **Inherits** *baseclass* [ **,** *baseclass* ] ... ]  
> &nbsp;&nbsp;&nbsp;&nbsp;[ *classmember* ]  
> &nbsp;&nbsp;&nbsp;&nbsp;[ *classmember* ] ...  
> **End Class**

*attributes*
: *optional* One or more of:  
[ArrayBoundsChecks](/tB/Core/Attributes.md#arrayboundschecks), [ClassId](/tB/Core/Attributes.md#classid), [COMCreatable](/tB/Core/Attributes.md#comcreatable), [CustomControl](/tB/Core/Attributes.md#customcontrol), [Description](/tB/Core/Attributes.md#description), [FloatingPointErrorChecks](/tB/Core/Attributes.md#floatingpointerrorchecks), [FormDesignerId](/tB/Core/Attributes.md#formdesignerid), [Hidden](/tB/Core/Attributes.md#hidden), [IntegerOverflowChecks](/tB/Core/Attributes.md#integeroverflowchecks), [PredeclaredID](/tB/Core/Attributes.md#predeclaredid)

**Public**
: *optional* (twinBASIC) In an ActiveX project, marks the class as exported into the type library so that consumers in other projects can create and use it.

**Private**
: *optional* (twinBASIC) In an ActiveX project, withholds the class from the type library: it remains usable within the project but is not exported. The conventional pairing with [**CoClass**](/tB/Core/CoClass.md) --- a public **CoClass** as the consumer-visible contract paired with a `Private Class` as the hidden implementation --- relies on this modifier.

*name*
: The identifier naming the class.

**Of** *typevars*
: *optional* (twinBASIC) One or more type variable names, separated by commas, that make the class a *generic class*. Each type variable can be referenced in member declarations as if it were a regular type. See [Generics](/Features/Language/Generics.md).

**Inherits** *baseclass*
: *optional* (twinBASIC) Names a base class whose **Public** and [**Protected**](/tB/Core/Protected.md) members are inherited by *name*. A class can inherit from more than one: list them after one **Inherits**, separated by commas, or give each its own **Inherits** line. The **Inherits** lines, when present, must appear immediately after the **Class** header and before any other member. **Inherits** enables [**Overridable**](/tB/Core/Sub.md) / **Overrides** members, explicit `*baseclass*.New(...)` chained constructor calls from inside `Sub New`, and **Protected** member visibility. See [Inheritance](/Features/Language/Inheritance.md).

*classmember*
: *optional* Any of the following:

  - [constant](/tB/Gloss.md#constant) defined using [**Const**](/tB/Core/Const.md),
  - [variable](/tB/Gloss.md#variable) defined using [**Public**](/tB/Core/Public.md), [**Protected**](/tB/Core/Protected.md), [**Private**](/tB/Core/Private.md), or [**Dim**](/tB/Core/Dim.md),
  - [procedure](/tB/Gloss.md#procedure) defined using [**Sub**](/tB/Core/Sub.md), [**Function**](/tB/Core/Function.md), or [**Property**](/tB/Core/Property.md) --- including the special instance constructor `Sub New(`*args*`)`, which the runtime invokes when the class is created with [**New**](/tB/Core/New.md),
  - [user-defined type (UDTs)](/tB/Gloss.md#user-defined-type) defined using [**Type**](/tB/Core/Type.md),
  - event defined using [**Event**](/tB/Core/Event.md) --- events may only be declared in a class module, and a [**WithEvents**](/tB/Core/WithEvents.md) field is how another module receives them,
  - (twinBASIC) [**Implements**](/tB/Core/Implements.md) clauses, listing interfaces or classes whose members this class provides bodies for.

In `.twin` files, a **Class** block may share a file with [**Interface**](/tB/Core/Interface.md), [**CoClass**](/tB/Core/CoClass.md), and [**Alias**](/tB/Core/Alias.md) declarations (which appear *before* the **Class** block) and with a [**Module**](/tB/Core/Module.md) block. In legacy `.cls` files the class is implicit and the **Class**/**End Class** keywords are not written.

### See Also

- [**Module** statement](/tB/Core/Module.md)
- [**Interface** statement](/tB/Core/Interface.md)
- [**CoClass** statement](/tB/Core/CoClass.md)
- [**Implements** statement](/tB/Core/Implements.md)
- [**Protected** statement](/tB/Core/Protected.md)
- [**New** statement](/tB/Core/New.md)
- [Inheritance](/Features/Language/Inheritance.md)
- [Generics](/Features/Language/Generics.md)

