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

Kotlin Q2 is wrong #4183

Closed
westito opened this issue Aug 8, 2022 · 5 comments · Fixed by #4559
Closed

Kotlin Q2 is wrong #4183

westito opened this issue Aug 8, 2022 · 5 comments · Fixed by #4559
Labels
good first issue Good for newcomers hacktoberfest-accepted help wanted Extra attention is needed

Comments

@westito
Copy link

westito commented Aug 8, 2022

https://github.com/Ebazhanov/linkedin-skill-assessments-quizzes/blob/main/kotlin/kotlin-quiz.md#q2-when-the-airplane-class-is-instantiated-it-displays-aircraft--null-not-aircraft--c130-why

Q2 is wrong. Abstract function can't be called (that's why it is abstract) so it can't return null. The answer closest to "A superclass is initialized before its subclass. Therefore, name has not been set before it is rendered", but this isn't really true either. If init{} in the Airplane class, then results will be the same.
The right answer is "init{} block runs before constructor". There is no reference linked and missing the instantiation line, C130 comes from nowhere.

@github-actions
Copy link

github-actions bot commented Aug 8, 2022

Hello @westito , thank you for submitting an issue! 👍 We highly appreciate it if you work on it as well.

@Ebazhanov Ebazhanov added help wanted Extra attention is needed good first issue Good for newcomers labels Aug 8, 2022
@hientranea
Copy link

I also had the same issue with this question. The correct answer should be A superclass is initialized before its subclass. Therefore, name has not been set before it is rendered

Here is the long explanation:

abstract class Aircraft {
  init { println("Aircraft = ${getName()}") }
  abstract fun getName(): String
}
class Airplane(private val name: String) : Aircraft() {
  override fun getName(): String = name
}

When we create a new instance of Airplane, here are these steps which Kotlin does exactly underhood:

val obj = Airplane("C130")
  1. Airplane's primary constructor is called. And it will trigger the primary constructor of the parent class first.
  2. Airplan doesn't have an explicit primary constructor. Thus, the secondary constructor (init block) is called then.
  3. In init block of Aircraft, the getName function of Airplane is called. And in this function, we access name field. This is the important key to the answer. The name field is only initialized until the primary constructor of Airplane is initialized completely. But we don't satisfy this condition at this time, so the null value returns instead.

By the way, if you pass the snippet code in IntelliJ IDEA, you will get the warning message from IDEA Calling non-final function getName in constructor. This is a common issue that is explained clearly in the Kotlin course made by JetBrains

@danielsacco
Copy link
Contributor

Hi, despite this issue is closed I would also suggest to add this as reference for Q2.

@Ebazhanov
Copy link
Owner

Hi, despite this issue is closed I would also suggest to add this as reference for Q2.

Thanks for suggestion @westito, you can simply raise a PR with your suggestion/changes. Thank you in advance!

@danielsacco
Copy link
Contributor

Done.
I didn't want to delete the reference to this interesting discussion, so I kept it as a "discussion" reference but it doesn't follow the same format as the rest of the questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers hacktoberfest-accepted help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants