---
title: "Classes"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Classes

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/ruby.md), and [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/swift.md).

A class is a user-defined type that holds a list of fields, like a [struct](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures.md). You define a class with the `class` keyword:

```slice
module M
{
    class TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}
```

Unlike a struct, a class can be empty.

Classes offer capabilities that structs do not:

- **Extensibility** You can extend a class through [inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) and [optional fields](https://docs.zeroc.com/ice/3.8/slice/fields.md#optional-fields).
- **Graph preservation** Ice preserves the references between class instances: two references to the same instance in a [class graph](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/self-referential-classes.md) still refer to one instance after unmarshaling.
- **Null value** A class parameter or field can be null, whereas a struct parameter or field cannot.
- **Slicing** With the [sliced format](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions.md#slice-formats), a receiver can unmarshal an instance of a derived class it does not know as the most-derived base class it knows.

Marshaling a class takes more time and space than marshaling a struct, so use a class only when you need one of these capabilities.

> **Note**
>
> A class represents data that you transmit over the wire, just like a struct. You can't define operations on a Slice class or implement an interface with a Slice class.

## Language Mapping

> **C++**
>
> A Slice class is mapped to a C++ class with the same name. The generated class contains a public data member for each Slice field (just as for [structures](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/cpp.md) and [exceptions](https://docs.zeroc.com/ice/3.8/slice/exceptions/cpp.md)). Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
>     string tz;          // e.g. GMT, PST, EDT...
> }
> ```
>
> The Slice compiler generates the following code for this definition:
>
> ```cpp
> class TimeOfDay;
> using TimeOfDayPtr = std::shared_ptr<TimeOfDay>;
>
> class TimeOfDay : public Ice::Value
> {
> public:
>     TimeOfDay() noexcept = default;
>     TimeOfDay(
>         std::int16_t hour,
>         std::int16_t minute,
>         std::int16_t second,
>         std::string tz) noexcept;
>
>     [[nodiscard]] TimeOfDayPtr ice_clone() const;
>
>     std::int16_t hour;
>     std::int16_t minute;
>     std::int16_t second;
>     std::string tz;
> };
> ```
>
> There are a number of things to note about this generated code:
>
> 1. The generated class `TimeOfDay` inherits from `Ice::Value`. `Ice::Value` is the ultimate ancestor of all mapped classes.
> 2. The generated class contains a public data member for each Slice field.
> 3. The generated class has a constructor that takes one argument for each data member, as well as a default constructor.
> 4. The generated class has a function, `ice_clone`, which returns a shallow polymorphic copy of this class instance.
>
> ### Generated Constructors
>
> Classes have two constructors:
>
> - **Default constructor** Default-constructs each data member. This constructor is `= default` and does not initialize a data member of type `bool`, of an integer, floating-point or enumeration type. The Slice compiler generates a data member initializer for each field with a [default value](https://docs.zeroc.com/ice/3.8/slice/fields/cpp.md#default-values).
> - **One-shot constructor** Takes one parameter for each data member, so you can construct and initialize a class instance in a single statement.
>
> For derived classes, the one-shot constructor has one parameter for each of the base class's data members, plus one parameter for each of the derived class's data members, in base-to-derived order. For example:
>
> ```slice
> class Base
> {
>     int i;
> }
>
> class Derived extends Base
> {
>     string s;
>     string greeting = "hello";
> }
> ```
>
> This generates:
>
> ```cpp
> class Base;
> using BasePtr = std::shared_ptr<Base>;
>
> class Derived;
> using DerivedPtr = std::shared_ptr<Derived>;
>
> class Base : public Ice::Value
> {
> public:
>     Base() noexcept = default;
>     explicit Base(std::int32_t i) noexcept;
>
>     [[nodiscard]] BasePtr ice_clone() const;
>
>     std::int32_t i;
> };
>
> class Derived : public Base
> {
> public:
>     Derived() noexcept = default;
>     Derived(std::int32_t i, std::string s, std::string greeting) noexcept;
>
>     [[nodiscard]] DerivedPtr ice_clone() const;
>
>     std::string s;
>     std::string greeting{"hello"};
> };
> ```
>
> Note that single-parameter constructors are defined as `explicit`, to prevent implicit argument conversions.
>
> ### Printing Classes
>
> You can print any class instance by calling `ice_print` on this instance. `ice_print` is defined on `Ice::Value`. Alternatively, you can print a shared pointer to a class instance (for example, a `TimeOfDayPtr`) with `operator<<`:
>
> ```cpp
> TimeOfDayPtr breakTime = ...;
> cout << "Taking a break at " << breakTime << endl;
> ```
>
> `operator<<` just calls `Value::ice_print` when the shared pointer is not null.
>
> You can use the metadata directive `"cpp:custom-print"` to tell the Slice compiler that you want to use your own custom print implementation. For example:
>
> ```slice
> ["cpp:custom-print"]
> class TimeOfDay { ... }
> ```
>
> The Slice compiler then generates an `ice_print` override declaration in the mapped C++ class, and you are responsible to implement this member function.

> **C#**
>
> A Slice class is mapped to a C# class with the same name. By default, the generated class contains a public field for each Slice field (just as for structures and exceptions). Alternatively, you can use the property mapping by specifying the `"cs:property"` metadata directive, which generates classes with properties instead of fields.
>
> Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     ["cs:identifier:Hour"]
>     short hour;         // 0 - 23
>
>     ["cs:identifier:Minute"]
>     short minute;       // 0 - 59
>
>     ["cs:identifier:Second"]
>     short second;       // 0 - 59
>
>     ["cs:identifier:TZ"]
>     string tz;          // e.g. GMT, PST, EDT...
> }
> ```
>
> The Slice compiler generates the following code for this definition:
>
> ```csharp
> public partial class TimeOfDay : Ice.Value
> {
>     public short Hour;
>     public short Minute;
>     public short Second;
>     public string TZ = "";
>
>     partial void ice_initialize();
>
>     public TimeOfDay(short Hour, short Minute, short Second, string TZ)
>     {
>         this.Hour = Hour;
>         this.Minute = Minute;
>         this.Second = Second;
>         global::System.ArgumentNullException.ThrowIfNull(TZ);
>         this.TZ = TZ;
>         ice_initialize();
>     }
>
>     public TimeOfDay()
>     {
>         ice_initialize();
>     }
>
>     // ...
> }
> ```
>
> There are a number of things to note about the generated code:
>
> 1. The generated class `TimeOfDay` inherits from `Ice.Value`. This means that all classes implicitly inherit from `Value`, which is the ultimate ancestor of all classes.
> 2. The generated class contains a public field for each Slice field.
> 3. The generated class has a constructor with a parameter for each field, and a parameterless constructor.
>
> ### Generated Constructors
>
> A generated class has a constructor with a parameter for each field, including inherited fields, so that you can create and initialize an instance in a single statement:
>
> ```csharp
> var tod = new TimeOfDay(14, 45, 00, "PST"); // 2:45pm
> ```
>
> It also has a parameterless constructor, which initializes the fields to their default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/csharp.md)). An empty class has only this constructor.
>
> When a class has non-optional fields whose type is a sequence, a dictionary, or a struct mapped to a C# class, the generated class replaces the parameterless constructor with a constructor that has a parameter for each of these fields and initializes the other fields to their default values. The parameterless constructor remains public for unmarshaling, but is hidden from IntelliSense.
>
> ### Property Mapping
>
> You can instruct the compiler to emit property definitions instead of public fields. For example:
>
> ```slice
> ["cs:property"] class Point
> {
>     ["cs:identifier:X"]
>     double x;
>
>     ["cs:identifier:Y"]
>     double y;
> }
> ```
>
> The `"cs:property"` metadata directive causes the compiler to generate a property for each Slice field:
>
> ```csharp
> public partial class Point : Ice.Value
> {
>     public double X { get; set; }
>     public double Y { get; set; }
>
>     // ...
>     // same as without cs:property
> }
> ```

> **Java**
>
> A Slice class is mapped to a Java class with the same name. The generated class contains a public field for each Slice field (just as for structures and exceptions). Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
>     string tz;          // e.g. GMT, PST, EDT...
> }
> ```
>
> The Slice compiler generates the following code for this definition:
>
> ```java
> public class TimeOfDay extends com.zeroc.Ice.Value {
>     public short hour;
>     public short minute;
>     public short second;
>     public String tz;
>
>     public TimeOfDay() {
>         this.tz = "";
>     }
>
>     public TimeOfDay(short hour, short minute, short second, String tz) {
>         this.hour = hour;
>         this.minute = minute;
>         this.second = second;
>         this.tz = tz;
>     }
>
>     public TimeOfDay clone() {
>         return (TimeOfDay) super.clone();
>     }
>
>     // ...
> }
> ```
>
> There are several things to note about the generated code:
>
> 1. The generated class `TimeOfDay` inherits from `com.zeroc.Ice.Value`. This means that all classes implicitly inherit from `Value`, which is the ultimate ancestor of all classes.
> 2. The generated class contains a public field for each Slice field.
> 3. The generated class has a constructor with a parameter for each field, and a parameterless constructor.
>
> ### Generated Constructors
>
> A generated class has the following constructors:
>
> - a constructor with a parameter for each field of the class
> - a parameterless constructor that initializes all fields to their default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/java.md))
>
> An empty class has only the parameterless constructor.
>
> When a Slice class declares both optional and non-optional fields, the mapped Java class provides a third constructor that accepts arguments for just the non-optional fields; the optional fields are left unset.
>
> The constructor with a parameter for each field allows you to create and initialize an instance in a single statement, for example:
>
> ```java
> TimeOfDay tod = new TimeOfDay((short) 14, (short) 45, (short) 0, "PST"); // 2:45pm PST
> ```
>
> For a derived class, this constructor has a parameter for every field of the class, including inherited fields. For example, consider the definition from [Class Inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) once more:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
> }
>
> class DateTime extends TimeOfDay
> {
>     short day;          // 1 - 31
>     short month;        // 1 - 12
>     short year;
> }
> ```
>
> The constructors for the generated classes are as follows:
>
> ```java
> public class TimeOfDay extends ... {
>     public TimeOfDay() {}
>
>     public TimeOfDay(short hour, short minute, short second) {
>         this.hour = hour;
>         this.minute = minute;
>         this.second = second;
>     }
>
>     // ...
> }
>
> public class DateTime extends TimeOfDay {
>     public DateTime() {}
>
>     public DateTime(
>         short hour,
>         short minute,
>         short second,
>         short day,
>         short month,
>         short year) {
>         super(hour, minute, second);
>         this.day = day;
>         this.month = month;
>         this.year = year;
>     }
>
>     // ...
> }
> ```

> **JavaScript**
>
> A Slice class is mapped to a JavaScript class with the same name. For each Slice field, the JavaScript instance contains a corresponding field (just as for structures and exceptions). Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
>     string tz;          // e.g. GMT, PST, EDT...
> }
> ```
>
> The Slice compiler generates the following code for this definition:
>
> ```js
> // JavaScript generated code.
> class TimeOfDay extends Ice.Value {
>     constructor(hour = 0, minute = 0, second = 0, tz = "") {
>         super();
>         this.hour = hour;
>         this.minute = minute;
>         this.second = second;
>         this.tz = tz;
>     }
> }
> ```
>
> ```typescript
> // TypeScript generated definition.
> class TimeOfDay extends Ice.Value {
>     constructor(hour?: number, minute?: number, second?: number, tz?: string);
>
>     hour: number;
>     minute: number;
>     second: number;
>     tz: string;
> }
> ```
>
> There are a number of things to note about the generated code:
>
> 1. The generated `TimeOfDay` class inherits from `Ice.Value`.
> 2. The generated class provides a constructor that accepts a value for each field.
> 3. The generated class defines a JavaScript field for each Slice field.
>
> ### Generated Constructor
>
> The generated constructor has one parameter for each field. This allows you to construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its fields).
>
> For example:
>
> ```js
> const tod = new TimeOfDay(14, 45, 0, "PST"); // 2:45pm PST
> ```
>
> All these parameters have default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/js.md)).
>
> For a derived class, the constructor accepts an argument for every field of the class, including inherited fields. For example, consider the definition from [Class Inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) once more:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
> }
>
> class DateTime extends TimeOfDay
> {
>     short day;          // 1 - 31
>     short month;        // 1 - 12
>     short year;
> }
> ```
>
> The constructors generated for these classes are similar to the following:
>
> ```js
> class DateTime extends TimeOfDay {
>     constructor(hour, minute, second, day = 0, month = 0, year = 0) {
>         super(hour, minute, second);
>         this.day = day;
>         this.month = month;
>         this.year = year;
>     }
> }
> ```
>
> ```typescript
> class DateTime extends TimeOfDay {
>     constructor(
>           hour?: number,
>           minute?: number,
>           second?: number,
>           day?: number,
>           month?: number,
>           year?: number);
>
>     day: number;
>     month: number;
>     year: number;
> }
> ```

> **MATLAB**
>
> A Slice class is mapped to a MATLAB class with the same name. The generated class contains a public property for each Slice field (just as for structures and exceptions).
>
> Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     ["matlab:identifier:Hour"]
>     short hour;         // 0 - 23
>
>     ["matlab:identifier:Minute"]
>     short minute;       // 0 - 59
>
>     ["matlab:identifier:Second"]
>     short second;       // 0 - 59
>
>     ["matlab:identifier:TZ"]
>     string tz;          // e.g. GMT, PST, EDT...
> }
> ```
>
> The Slice compiler generates the following code for this definition:
>
> ```matlab
> classdef TimeOfDay < Ice.Value
>     properties
>         Hour    (1, 1) int16
>         Minute  (1, 1) int16
>         Second  (1, 1) int16
>         TZ      (1, :) char
>     end
>     methods
>         function obj = TimeOfDay(Hour, Minute, Second, TZ)
>             if nargin > 0
>                 assert(nargin == 4, 'Invalid number of arguments');
>                 % ...
>             end
>         end
>         % ...
>     end
> end
> ```
>
> There are several things to note about the generated code:
>
> 1. The generated class `TimeOfDay` inherits from `Ice.Value`. This means that all classes implicitly inherit from `Value`, which is the ultimate ancestor of all classes.
> 2. The generated class contains a public property for each Slice field.
> 3. The generated class has a constructor that takes one argument for each field.
>
> ### Generated Constructor
>
> If a Slice class declares or inherits any field, the generated constructor accepts one parameter for each property so that you can construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its properties). For a derived class, the constructor accepts one argument for each base class property, plus one argument for each derived class property, in base-to-derived order.
>
> You must either call the constructor with no arguments or with arguments for all of the parameters.
>
> Calling the constructor with no argument assigns default values to the properties (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/matlab.md)).

> **PHP**
>
> A Slice class maps to a PHP class with the same name. For each Slice field, the generated class contains a public property, just as for structures and exceptions. Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
> }
> ```
>
> The PHP mapping generates the following code for this definition:
>
> ```php
> class TimeOfDay extends \Ice\Value
> {
>     public $hour;
>     public $minute;
>     public $second;
>
>     public function __construct($hour=0, $minute=0, $second=0)
>     {
>         $this->hour = $hour;
>         $this->minute = $minute;
>         $this->second = $second;
>     }
> }
> ```
>
> There are a number of things to note about the generated code:
>
> 1. The generated class `TimeOfDay` inherits from `\Ice\Value`. This reflects the semantics of Slice classes in that all classes implicitly inherit from `\Ice\Value`, which is the ultimate ancestor of all classes.
> 2. The constructor initializes a property for each Slice field.
>
> ### Generated Constructor
>
> The generated constructor has one parameter for each field. This allows you to construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its properties).
>
> All these parameters have also default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/php.md)).
>
> For derived classes, the constructor has one parameter for each of the base class's fields, plus one parameter for each of the derived class's fields, in base-to-derived order.

> **Python**
>
> A Slice class maps to a Python dataclass with the same name. The generated class contains a field for each Slice field (just as for structures and exceptions). Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
> }
> ```
>
> The Python mapping generates the following code for this definition:
>
> ```py
> @dataclass(eq=False)
> class TimeOfDay(Value):
>     hour: int = 0
>     minute: int = 0
>     second: int = 0
>
>     # ...
> ```
>
> The generated class `TimeOfDay` inherits from `Ice.Value`. This means that all classes implicitly inherit from `Ice.Value`, which is the ultimate ancestor of all classes.
>
> All mapped fields have default values, such as `0` and the empty string (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/python.md) for details).
>
> The mapped dataclass is configured with `eq=False` to provide reference-equality semantics like in other language mappings: two class instances are equal only when they are actually the same instance.

> **Ruby**
>
> A Slice class maps to a Ruby class with the same name, capitalized. For each Slice field, the generated class contains an instance variable with read and write accessors, as for structures and exceptions. Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
> }
> ```
>
> The Ruby mapping generates the following code for this definition:
>
> ```ruby
> class TimeOfDay < ::Ice::Value
>     attr_accessor :hour, :minute, :second
>
>     def initialize(hour=0, minute=0, second=0)
>         @hour = hour
>         @minute = minute
>         @second = second
>     end
> end
> ```
>
> There are a number of things to note about the generated code:
>
> 1. The generated class `TimeOfDay` derives from `Ice::Value`. This reflects the semantics of Slice classes in that all classes implicitly inherit from `Ice::Value`, which is the ultimate ancestor of all classes.
> 2. The constructor defines an instance variable for each Slice field.
>
> ### Generated Constructor
>
> The generated constructor has one parameter for each field. This allows you to construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its variables).
>
> All these parameters have also default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/ruby.md)).
>
> For derived classes, the constructor has one parameter for each of the base class's fields, plus one parameter for each of the derived class's fields, in base-to-derived order.

> **Swift**
>
> A Slice class is mapped to an open Swift class with the same name. The generated class contains a public stored property for each Slice field (just as for structures and exceptions). Consider the following class definition:
>
> ```slice
> class TimeOfDay
> {
>     short hour;         // 0 - 23
>     short minute;       // 0 - 59
>     short second;       // 0 - 59
>     string tz;          // e.g. GMT, PST, EDT...
> }
> ```
>
> The Slice compiler generates the following code for this definition:
>
> ```swift
> open class TimeOfDay: Ice.Value {
>     public var hour: Int16 = 0
>     public var minute: Int16 = 0
>     public var second: Int16 = 0
>     public var tz: String = ""
>
>     public required init() {}
>
>     public init(hour: Int16, minute: Int16, second: Int16, tz: String) {
>         self.hour = hour
>         self.minute = minute
>         self.second = second
>         self.tz = tz
>     }
>     ...
> }
> ```
>
> There are several things to note about the generated code:
>
> 1. The generated class `TimeOfDay` inherits from class `Ice.Value`. `Value` is the ultimate ancestor of all classes.
> 2. The generated class contains a public stored property for each Slice field.
> 3. The generated class provides a default initializer and a memberwise initializer. The default initializer initializes all stored properties to zero, nil or empty, as appropriate. See [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/swift.md) for details.
