Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Object-Oriented Programming with Kotlin

You're reading from   Hands-On Object-Oriented Programming with Kotlin Build robust software with reusable code using OOP principles and design patterns in Kotlin

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781789617726
Length 370 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Abid Khan Abid Khan
Author Profile Icon Abid Khan
Abid Khan
Igor Kucherenko Igor Kucherenko
Author Profile Icon Igor Kucherenko
Igor Kucherenko
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with Kotlin 2. Introduction to Object-Oriented Programming FREE CHAPTER 3. The Four Pillars of Object-Oriented Programming 4. Classes - Advanced Concepts 5. Data Collection, Iterators, and Filters 6. Object-Oriented Patterns in Kotlin 7. Coroutines - a Lightweight Thread? 8. Interoperability 9. Regular Expression and Serialization in Kotlin 10. Exception Handling 11. Testing in Object-Oriented Programming with Kotlin 12. Assessments 13. Other Books You May Enjoy

Classes in Kotlin

In Kotlin, a class can be defined with the class keyword. Let's take a look at how to declare a class and how to add attributes to it. In Kotlin, a class can be declared as follows:

class Person 

Compared to other programming languages, creating a class in Kotlin is very easy. All we need to do is open our IDE and create a person class with three attributes—name, age, and height. The name is a variable of the String type, age is a variable of the Integer type, and height is a variable of the Double type:

class Person {
var name: String
var age : Int
var height : Double
}

We have now declared a Person class with three attributes enclosed in brackets. After writing this code, we will notice that compiler has immediately thrown a Property not initialized error, because we haven't decided that how these properties will be initialized. Let...

You have been reading a chapter from
Hands-On Object-Oriented Programming with Kotlin
Published in: Oct 2018
Publisher: Packt
ISBN-13: 9781789617726
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime