Skip to content

Update kotlin-tour-intermediate-open-special-classes.md #4909

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 1 commit into
base: master
Choose a base branch
from

Conversation

Geek-Joe
Copy link
Contributor

call to the constructor

@Geek-Joe Geek-Joe requested a review from a team as a code owner June 22, 2025 05:51
Copy link
Collaborator

@sarahhaggarty sarahhaggarty left a comment

Choose a reason for hiding this comment

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

Thanks for pointing this out! After some thought, I'd like to make a slightly bigger change than you are suggesting. Please take a look at my comment.

@@ -30,7 +30,7 @@ To create a class that inherits from another, add a colon after your class heade
of the parent class that you want to inherit from:

```kotlin
Copy link
Collaborator

Choose a reason for hiding this comment

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

After thinking about it, I'd like to remove this code snippet and bring the example sooner to make it clearer that the constructor is called. Can you apply my below suggestions?

Suggested change
```kotlin
If you can't use interfaces or abstract classes, you can explicitly make a class inheritable by declaring it as **open**.
To do this, use the `open` keyword before your class declaration:
```kotlin
open class Vehicle(val make: String, val model: String)
```
To create a class that inherits from another, add a colon after your class header followed by a call to the constructor
of the parent class that you want to inherit from. In this example, the `Car` class inherits from the `Vehicle` class:
```kotlin
open class Vehicle(val make: String, val model: String)
class Car(make: String, model: String, val numberOfDoors: Int) : Vehicle(make, model)
fun main() {
// Creates an instance of the Car class
val car = Car("Toyota", "Corolla", 4)
// Prints the details of the car
println("Car Info: Make - ${car.make}, Model - ${car.model}, Number of doors - ${car.numberOfDoors}")
// Car Info: Make - Toyota, Model - Corolla, Number of doors - 4
}
```

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