# Classes and objects

> Define constructors, fields, properties, instance methods, inheritance, and interfaces.

Canonical: https://alice-lang.org/documentation/language/classes/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

## Define a class

A class groups state and behavior. Construct an instance with `.new` and invoke its methods with a dot.

```alice
class Counter
  getter name : String
  property value : Int32 = 0
  def initialize(@name : String)
  end
  def increment
    @value += 1
  end
end
c = Counter.new("tea")
c.increment
puts "#{c.name}: #{c.value}"
```

Expected output:

```text
tea: 1
```

[Edit and run in the Alice playground](https://playground.alice-lang.org/#code=u.Y2xhc3MgQ291bnRlcgogIGdldHRlciBuYW1lIDogU3RyaW5nCiAgcHJvcGVydHkgdmFsdWUgOiBJbnQzMiA9IDAKICBkZWYgaW5pdGlhbGl6ZShAbmFtZSA6IFN0cmluZykKICBlbmQKICBkZWYgaW5jcmVtZW50CiAgICBAdmFsdWUgKz0gMQogIGVuZAplbmQKYyA9IENvdW50ZXIubmV3KCJ0ZWEiKQpjLmluY3JlbWVudApwdXRzICIje2MubmFtZX06ICN7Yy52YWx1ZX0i)

`initialize` is the constructor. `@name` in its parameter list both accepts the argument and assigns the instance field. `getter name : String` exposes a read-only property, while `property value : Int32 = 0` exposes a read-write property with a default.

A method can assign `@field` directly. Its type can be inferred from the assignment. A declaration without a type or initializer may need an explicit annotation (AL0205).

## Accessors and aliases

| Form                      | Purpose                                               |
| ------------------------- | ----------------------------------------------------- |
| `getter x : T`            | Read-only property                                    |
| `setter x : T`            | Write access                                          |
| `property x : T`          | Read and write access                                 |
| `attr_reader :x`          | Ruby-style reader, inferred from field initialization |
| `attr_writer :x`          | Ruby-style writer                                     |
| `attr_accessor :x, :y`    | Readers and writers                                   |
| `alias new_name old_name` | Forwarding method alias                               |

Use getters for state callers should observe, and explicit methods when changing state has rules. A read-only property does not make every reachable object immutable.

## Static methods

`def self.name` defines a method called on the class rather than an instance. Class state uses `@@name`; it is shared, so concurrent access needs the same care as other shared mutable state.

## Inheritance and super

```alice
class Animal
  def voice : String
    "hello"
  end
end
class Rabbit < Animal
  def voice : String
    super + " from rabbit"
  end
end
puts Rabbit.new.voice
```

`super` calls the base implementation. Alice seals leaf classes, and emits virtual methods when needed for Alice subclasses. Do not assume every method has dynamic dispatch simply because the syntax is Ruby-inspired.

You can also inherit .NET classes. Inherited members can be called unqualified inside the subclass. Existing .NET virtual members, including protected ones, can be overridden with compatible signatures. See [.NET interop](https://alice-lang.org/documentation/guides/dotnet.md).

## Interfaces

Declare an `interface` and implement it with `class Name < InterfaceName`. Match the method contracts. Generic constraints can require an interface such as `IComparable(T)`; the generic chapter gives a complete example.

The parser accepts access modifiers such as `abstract` and `private`, but these forms have less compiler-test coverage than the core object model. Prefer the documented getter/property and interface forms for stable APIs, and verify modifier-dependent behavior with your selected compiler.

## Choose an appropriate type

Use a class when identity and shared mutable state matter, a [record](https://alice-lang.org/documentation/language/records.md) for named data with value equality, a struct for value semantics, and an [enum with variants](https://alice-lang.org/documentation/language/pattern-matching.md) for a closed set of alternatives.

## Continue reading

- Previous: [Blocks, yield, and lambdas](https://alice-lang.org/documentation/language/blocks.md)
- Next: [Structs and records](https://alice-lang.org/documentation/language/records.md)
