Skip to content

update: improve Classes page #4786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

AlejandraPedroza
Copy link
Contributor

@AlejandraPedroza AlejandraPedroza commented Apr 2, 2025

This PR revamps the content and code examples in the Classes page following external feedback.
Some of the key changes are:

  • Improve the whole page and revamp the code snippets.

  • Explain the use case for multiple init blocks.

  • Explain constructor chaining.

  • Add examples for primary constructors.

  • Clarify the open keyword.

  • Explain how to access a class property from a class member function.

  • Use the term "object" consistently.

  • Clarify what abstract classes are.

  • Provide example companion objects.

@AlejandraPedroza AlejandraPedroza requested a review from a team as a code owner April 2, 2025 14:13
Copy link

@ice-phoenix ice-phoenix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main question is: what are the things we want the reader to know, after they've finished reading the updated "Classes" page? Is it basic syntax for classes, details about constructors, details about initialization, smth else? Right now the section is a bit all over the place, I feel.

In other words, when we set to improve the page, what did we want to achieve?

and some other things), and the class body surrounded by curly braces. Both the header and the body are optional; if the
class has no body, the curly braces can be omitted.
The class declaration consists of:
* **Class header**, including:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it "including" or "including but not limited to"? Because class header also includes, for example, supertype list.


```kotlin
class Person constructor(firstName: String) { /*...*/ }
// Class with only a name and no body
class Person

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better example would be a class with a primary constructor and some properties, but no body

If the primary constructor does not have any annotations or visibility modifiers, the `constructor` keyword can be omitted:
## Constructors and initializer blocks

A class in Kotlin has a [_primary constructor_](#primary-constructor) and, optionally, one or more [_secondary constructors_](#secondary-constructors).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we correctly said primary ctor is optional. Here I'm reading as if it is mandatory. I would suggest trying to find a better phrasing for the idea "primary ctor is optional, but we believe in most cases your classes should have one".

}
```

See classes in action. Considering the same code example, if you [create an object](#creating-objects-of-classes) of the `Person` class by passing values for

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"See classes in action"?

) { /*...*/ }
```

Using the trailing comma example, if you [create an object](#creating-objects-of-classes) of the `Person`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not fully get what we say via "using the trailing comma example" here

> Kotlin does not have a `new` keyword.
>
{style="note"}
Typically, you create objects within the `main()` function because it serves as the entry point of your Kotlin program.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement is not true, you create objects whenever you need to do that, and not only within the main function

You don't need to annotate abstract classes or functions with the `open` keyword because they are implicitly
open and designed to be inherited.

Similarly, you can override a non-abstract `open` member with an abstract one:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, I do not think this example adds anything useful for one reading the documentation. Such pattern of overriding open with abstract is very strange and should not be used unless there are very specific circumstances.


You can override a non-abstract `open` member with an abstract one.
## Open keyword

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say this section makes more sense in the Inheritance part of the documentation


Even more specifically, if you declare a [companion object](object-declarations.md#companion-objects) inside your class,
If you need to write a function that can be called without having a class object but still needs access
to the internals of the class, you can define it inside `companion object` within that class:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you define "internals of the class" here? A companion object can access its own internals, but class itself does not have any internals until you have created an instance of it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, here the terminology begins being a bit confusing, as "class object" and "companion object" look very similar, but mean significantly different things

Copy link
Contributor Author

@AlejandraPedroza AlejandraPedroza Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you define "internals of the class" here?

This comes in the current version of the docs and I didn't modify it, but I rephrased it. What do you think?

@@ -1,245 +1,747 @@
[//]: # (title: Classes)

Classes in Kotlin are declared using the keyword `class`:
Like other object-oriented languages, Kotlin uses _classes_ to encapsulate data and behavior
for reusable, structured code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing a short intro talking about different "parts" of a class, similar to what you have in one of the later sections. Smth like "Classes encapsulate data (properties) and behavior (functions). They are templates for objects, which you create via constructors."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer to see a very simple example in the beginning of "class -> ctor -> created object instance" which we then iteratively expand on, by adding more properties, functions, ctors, init blocks or other things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants