Interface and Abstract Classes
Kotlin Study Group
John
Hi, I am John
- Android Developer
- Work on Rakuten Viki
- Use Kotlin for 2 years
- Linkedin:
https://www.linkedin.com/in/chang-john-0a95237a/
Outline
- Preface
- Interfaces
- Default Implementation
- Abstract Classes
- Difference
Preface
- Be aware the discrepancy of interface between Java(8 later and 8 prior) and Kotlin
- Be aware how to implement interface and abstract class between Java and Kotlin
- Be aware different between interface and abstract class in Kotlin
- Know about scenario of interface and abstract class usage
Interface - declaration
Interface - implementation
Interface - default implementation
Properties in interfaces
Computed Property
Property
Resolving overriding conflicts
default implementation
C only need to override bar because A already provide
default implementation
When interface has same signature function, whatever interface has default
implementation, D still override explicitly.
If D want to call default implementation, use super<T>.function(), like
super<A>.foo()
Discrepancy between Kotlin and Java
Kotlin Java 8 Java 8 prior
Field Final static via companion
object
Final static Final static
Property(field+accessor) Yes(but no backing field) No No
Function abstract/default/static abstract/default/static abstract
Abstract Classes
Difference
- Interface is stateless(they can’t have a state), abstract class is stateful(they can
have a state)
- An interface cannot specify a constructor.
- Interface can provide default implementation, but abstract function in abstract
class can’t have body
- A class can extend only one abstract class, but can implement many interface.
- From design view, interface is contract and abstract class is “is-a”

Interface and abstract classes

  • 1.
    Interface and AbstractClasses Kotlin Study Group John
  • 2.
    Hi, I amJohn - Android Developer - Work on Rakuten Viki - Use Kotlin for 2 years - Linkedin: https://www.linkedin.com/in/chang-john-0a95237a/
  • 3.
    Outline - Preface - Interfaces -Default Implementation - Abstract Classes - Difference
  • 4.
    Preface - Be awarethe discrepancy of interface between Java(8 later and 8 prior) and Kotlin - Be aware how to implement interface and abstract class between Java and Kotlin - Be aware different between interface and abstract class in Kotlin - Know about scenario of interface and abstract class usage
  • 5.
  • 6.
  • 7.
    Interface - defaultimplementation
  • 8.
  • 9.
    Resolving overriding conflicts defaultimplementation C only need to override bar because A already provide default implementation When interface has same signature function, whatever interface has default implementation, D still override explicitly. If D want to call default implementation, use super<T>.function(), like super<A>.foo()
  • 10.
    Discrepancy between Kotlinand Java Kotlin Java 8 Java 8 prior Field Final static via companion object Final static Final static Property(field+accessor) Yes(but no backing field) No No Function abstract/default/static abstract/default/static abstract
  • 11.
  • 12.
    Difference - Interface isstateless(they can’t have a state), abstract class is stateful(they can have a state) - An interface cannot specify a constructor. - Interface can provide default implementation, but abstract function in abstract class can’t have body - A class can extend only one abstract class, but can implement many interface. - From design view, interface is contract and abstract class is “is-a”