Kotlin For Beginners - Variance, Covariance, Contravariance and Type erasure

Charfaoui Younes
5 Jun 202213:00

Summary

TLDRIn this video, the speaker explains the concepts of variance, covariance, contravariance, and invariance in object-oriented programming, particularly in Java and Kotlin. The video explores how inheritance works with generics, emphasizing the differences between classes and types. It highlights key issues like why a list of integers isn't a subtype of a list of numbers and explains the use of 'in' and 'out' keywords in Kotlin to manage variance. The video also covers type erasure, a common problem in Java and Kotlin, and offers practical examples to demonstrate these concepts clearly. The focus is on understanding how generics work and ensuring correct usage in programming.

Takeaways

  • 📚 Understanding variance is important when learning about object-oriented programming (OOP), specifically inheritance.
  • 🔢 In Java, types like integers, floats, and doubles inherit from the Number class, but lists of integers or doubles are not subtypes of lists of numbers.
  • 🚫 Java doesn't allow assigning a list of integers to a list of numbers due to invariance. Kotlin, however, allows this assignment.
  • 🛠️ Kotlin and Java handle generic types differently. In Kotlin, a read-only list works differently from a mutable list.
  • ⚖️ Covariance allows using a subtype in place of a supertype. For example, you can use a list of admins and normal users as a list of users.
  • 🔄 Contravariance is the opposite of covariance, allowing the use of a supertype in place of a subtype. This is commonly used in comparators.
  • 🔑 The 'out' keyword in Kotlin is used for covariance, while the 'in' keyword is used for contravariance.
  • 🔒 Invariance means that a list of integers is not a subtype of a list of numbers by default.
  • 🛑 Type erasure in Java and Kotlin occurs at runtime, meaning the specific type information (like list of integers or list of strings) is not retained.
  • 💡 To avoid conflicts with type erasure in Kotlin, the @JvmName annotation can be used to rename overloaded functions to prevent runtime errors.

Q & A

  • What is the main focus of the video?

    -The video focuses on explaining the concepts of variance, including covariance, contravariance, and invariance, especially in the context of object-oriented programming (OOP) with examples in Java and Kotlin.

  • What is variance in object-oriented programming?

    -Variance describes how subtyping between more complex types (like generic types) relates to subtyping between their components. It explains whether you can substitute types of one kind with types of another kind.

  • Why can't a 'List of Integer' be assigned to a 'List of Number' in Java?

    -In Java, a 'List of Integer' is not considered a subtype of 'List of Number' due to invariance. Even though Integer is a subtype of Number, a list of integers is not a subtype of a list of numbers.

  • How does Kotlin handle variance differently from Java?

    -Kotlin allows assigning a 'List of Integer' to a 'List of Number' without issues due to its variance system. Kotlin uses keywords like 'in' and 'out' to handle variance more flexibly than Java.

  • What is covariance in the context of generics?

    -Covariance means that a generic type is declared to preserve the relationship between a type and its subtype. For example, in Kotlin, a 'List<out T>' allows a list of a subtype (e.g., Integer) to be assigned to a list of its supertype (e.g., Number).

  • What is contravariance?

    -Contravariance is the opposite of covariance. It means that a type can accept a supertype in the place of a subtype. For example, in Kotlin, a 'Comparator<in T>' can compare a supertype like 'User' with a subtype like 'Administrator'.

  • What is the significance of the 'out' keyword in Kotlin?

    -The 'out' keyword in Kotlin is used to declare a type parameter as covariant. It means that the type can be used as a return type but not as an argument type. This ensures that the type can only produce values, maintaining the covariant property.

  • How is contravariance represented in Kotlin?

    -Contravariance in Kotlin is represented using the 'in' keyword, which indicates that a type can be used as an input but not as an output. This allows a more general type (supertype) to be used where a more specific type (subtype) is expected.

  • What is the default variance in Java's generic types?

    -The default variance in Java's generic types is invariance. This means that a 'List<Integer>' is not considered a subtype of 'List<Number>', even though Integer is a subtype of Number.

  • What is type erasure, and why does it cause issues in overloading functions in Java and Kotlin?

    -Type erasure is a process where generic type information is removed at runtime, meaning that types like 'List<Integer>' and 'List<String>' are treated as just 'List'. This causes issues when overloading methods with different generic types, as they end up having the same signature at runtime.

Outlines

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Mindmap

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Keywords

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Highlights

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Transcripts

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级
Rate This

5.0 / 5 (0 votes)

相关标签
OOP conceptsJava programmingKotlin basicsCovarianceContravarianceInheritanceGenericsCoding tutorialsVariance in JavaProgramming tips
您是否需要英文摘要?