Skip to content
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

KTLN-484 Resolving Kotlin Error: Primary constructor call expected #1152

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.resolvePrimaryConstructorCallExpectedIssue

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class resolvePrimaryConstructorCallExpectedIssueUnitTest {

@Test
fun `correctly resolve compile error by properly defining constructors`() {
val student = Student("Martial", 15)

assertEquals("Martial", student.name)
assertEquals(15, student.age)
}

@Test
fun `correctly resolve compile error by properly using the secondary constructor`() {
val dog = Dog("Max")
assertEquals("Max", dog.species)
}

@Test
fun `correctly resolve compile error by properly using default parameters`() {
val car1 = Car("Toyota", "Corolla")

assertEquals("Toyota", car1.make)
assertEquals("Corolla", car1.model)

}
}

class Student(var name: String) {
var age: Int = 14

constructor(name: String, age: Int) : this(name) {
this.age = age
}

fun printMsg() {
println("Name is $name. Age is $age.")
}
}

open class Animal(val species: String)

class Dog(species: String) : Animal(species)

class Car(val make: String, val model: String = "Unknown")