> ## Documentation Index
> Fetch the complete documentation index at: https://gluals.arnux.net/llms.txt
> Use this file to discover all available pages before exploring further.

# @class

> Define classes with inheritance, generics, and field access control.

## Overview

`@class` defines a named type that other annotations can use. Classes support inheritance, generic type parameters, access control, and field definitions.

***

## Syntax

```lua theme={null}
---@class ClassName
---@class ClassName : ParentClass
---@class ClassName : Parent1, Parent2
---@class (exact) ClassName
---@class (partial) ClassName
---@class ClassName<T>
```

***

## Basic class <a id="basic-class" />

```lua theme={null}
---@class Animal
---@field name string Animal name
---@field species string Species
local Animal = {}

function Animal:Speak()
    print(self.name .. " makes a sound")
end
```

***

## Inheritance <a id="inheritance" />

Use `: ParentClass` to extend another class. The child class gets the parent's fields and methods.

```lua theme={null}
---@class Dog : Animal
---@field breed string Dog breed
local Dog = setmetatable({}, { __index = Animal })

function Dog:Fetch(item)
    -- inherits Animal fields: self.name, self.species ✅
end
```

### Multiple inheritance <a id="multiple-inheritance" />

```lua theme={null}
---@class Flyable
---@field maxAltitude number

---@class Swimmable
---@field maxDepth number

---@class Duck : Animal, Flyable, Swimmable
---@field featherColor string
```

***

## Exact classes <a id="exact-classes" />

Mark a class as `(exact)` to prevent dynamic field additions. GLuaLS can report a diagnostic when code assigns a field outside the class definition:

<Info>
  In GLuaLS, this annotation has no effect. GLuaLS treats all classes as partial by default and turns related diagnostics off. GLuaLS keeps it for EmmyLua compatibility.
</Info>

```lua theme={null}
---@class (exact) Point
---@field x number
---@field y number

local p = Point.new(1, 2)
p.z = 3 -- ❌ Error: cannot add field 'z' to exact type Point
```

***

## Partial classes <a id="partial-classes" />

`(partial)` extends an existing class without relisting all fields:

<Info>
  In GLuaLS, this annotation has no effect. GLuaLS treats all classes as partial by default and turns related diagnostics off. GLuaLS keeps it for EmmyLua compatibility.
</Info>

```lua theme={null}
-- In one file:
---@class Animal
---@field name string

-- In another file:
---@class (partial) Animal
---@field weight number  -- adds weight to Animal without losing name
```

***

## Generic classes <a id="generic-classes" />

Add type parameters with angle brackets. You can use type parameters in field types:

```lua theme={null}
---@class Container<T>
---@field items T[]
---@field capacity number

---@type Container<string>
local stringContainer = { items = {}, capacity = 10 }
stringContainer.items[1] = "hello" -- ✅ typed as string
```

***

## Field access control <a id="field-access-control" />

Use access modifiers in `@field`:

<Info>
  Access modifiers are optional. They affect type checking only, not runtime behavior. GLuaLS checks them and can report diagnostics when code uses private or protected fields from the wrong place.
</Info>

```lua theme={null}
---@class MyClass
---@field public name string Accessible everywhere
---@field private m_id number Only accessible in class methods
---@field protected m_parent MyClass Accessible in subclasses
```

See [@field](/annotations/field) for details.

***

## Attaching a class to a local variable <a id="attaching-a-class-to-a-local-variable" />

When you place `---@class` above a `local` statement, GLuaLS types that variable as an instance of the class:

```lua theme={null}
---@class Player
local Player = {}
-- Player is now typed as "instance of Player class"
```

To type a *constructor* table instead, use `@type`:

```lua theme={null}
local Player = {}
---@type Player
local ply = Player.new()
```
