Defining 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
- How to define a class?
- How to construct an instance?
- What’re class function and class property?
- Visibility and Encapsulation
- Properties
- Class property(getter and setter)
- Computed property
- Use Packages
- Guarding Against Race Conditions
Preface
Why we want to build object-oriented paradigm?
- in order to manage complexity
Why object-oriented paradigm deal with complexity easily?
- Not related with encapsulation, inheritance, polymorphism. It’s abstraction.
What’s abstraction?
- Distinguish what it need and what it doesn’t need when defining a class
Preface
What’s difference between Java and Kotlin for object-oriented-paradigm?
- Java forces developer implements explicitly. Kotlin hopes developer adopt
idiomatic(implicit)
For this book, What’s learning path for object-oriented-paradigm?
- Ch12, build a class(field vs property)
- Ch13, Initialization (primary, secondary, init block)
- Ch14, Inheritance (casting)
- Ch15, Object (different kinds of class)
- Ch16, Interfaces and Abstract Classes (Discrepancy)
How to define a class?
How to construct an instance?
What’re class function and class property?
Visibility and Encapsulation
- By default, any function and property without a visibility modifier is Public
- By default, Java uses Package Private visibility
-
Properties = field + accessor
What’s advantage of Properties?
1. Distinguish function and property more clearly
2. Related logic lives more close
3. Customization becomes more easy
4. Kotlin make it idiomatic, write less code
- Property can distinguish class property and computed property in class
- Class property Must assign property initializer so that compiler know that it must
create a field for this property
- Computed property MUST NOT give property initializer because its reference is
from result or delegation. Therefore, it has no field.
- var for writable(mutable) and val for read-only(immutable).
- var property owns getter/setter and val property only owns getter.
Property Example
This is class property
This is computed property
Property Example
This is class property without modifying getter/setter
This is val class property so it only getter
Use Package
Guarding Against Race Conditions
Reference
- Kotlin Programmer Dictionary: Field vs Property
-

Defining classes

  • 1.
  • 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 - Howto define a class? - How to construct an instance? - What’re class function and class property? - Visibility and Encapsulation - Properties - Class property(getter and setter) - Computed property - Use Packages - Guarding Against Race Conditions
  • 4.
    Preface Why we wantto build object-oriented paradigm? - in order to manage complexity Why object-oriented paradigm deal with complexity easily? - Not related with encapsulation, inheritance, polymorphism. It’s abstraction. What’s abstraction? - Distinguish what it need and what it doesn’t need when defining a class
  • 5.
    Preface What’s difference betweenJava and Kotlin for object-oriented-paradigm? - Java forces developer implements explicitly. Kotlin hopes developer adopt idiomatic(implicit) For this book, What’s learning path for object-oriented-paradigm? - Ch12, build a class(field vs property) - Ch13, Initialization (primary, secondary, init block) - Ch14, Inheritance (casting) - Ch15, Object (different kinds of class) - Ch16, Interfaces and Abstract Classes (Discrepancy)
  • 6.
  • 7.
    How to constructan instance?
  • 8.
    What’re class functionand class property?
  • 9.
    Visibility and Encapsulation -By default, any function and property without a visibility modifier is Public - By default, Java uses Package Private visibility -
  • 10.
  • 11.
    What’s advantage ofProperties? 1. Distinguish function and property more clearly 2. Related logic lives more close 3. Customization becomes more easy 4. Kotlin make it idiomatic, write less code
  • 12.
    - Property candistinguish class property and computed property in class - Class property Must assign property initializer so that compiler know that it must create a field for this property - Computed property MUST NOT give property initializer because its reference is from result or delegation. Therefore, it has no field. - var for writable(mutable) and val for read-only(immutable). - var property owns getter/setter and val property only owns getter.
  • 13.
    Property Example This isclass property This is computed property
  • 14.
    Property Example This isclass property without modifying getter/setter This is val class property so it only getter
  • 15.
  • 16.
  • 17.
    Reference - Kotlin ProgrammerDictionary: Field vs Property -