Subscribe for notification
Categories: Android Tutorial

Creating Class with Properties & Member Functions in Kotlin

Kotlin’s object model is substantially different from Python’s. Most importantly, classes are not dynamically modifiable at runtime! (There are some limited exceptions to this, but you generally shouldn’t do it. However, it is possible to dynamically inspect classes and objects at runtime with a feature called reflection – this can be useful, but should be judiciously used.) All properties (attributes) and functions that might ever be needed on a class must be declared either directly in the class body or as extension functions, so you should think carefully through your class design.

Kotlin supports features such as higher-order functions, function types and lambdas which makes it a great choice for working in functional programming style. You will learn about these concept in later chapters. This article will focus on object-oriented style of programming in Kotlin.

To be fair, you’d get the same output in Python, but the mechanism would be different: both instances would start out without any attributes of their own (age and name would be attributes on the class), and the first printing would access the class attribute; only the assignment would cause an age attribute to appear on a. In Kotlin, there are no class properties in this example, and each instance starts out with both properties. If you need a class-level property, see the section on companion objects.

Kotlin class is similar to Java class, a class is a blueprint for the objects which have common properties. Kotlin classes are declared using keyword class. Kotlin class has a class header which specifies its type parameters, constructor etc. and the class body which is surrounded by curly braces.

Because the set of properties of an object is constrained to be exactly the set of properties that are declared at compile-time in the object’s class, it’s not possible to add new properties to an object or to a class at runtime, so e.g. a.nationality = “Norwegian” won’t compile.

Kotlin supports both functional and object-oriented programming. In previous articles, we have learned about functions, higher-order functions and lambdas which represents Kotlin as a functional language. Here, we will learn about the basic OOPs concepts which represent Kotlin as Object-Oriented programming language.

Object-Oriented Programming Language –
Class and Objects are the basic concepts of object-oriented programming language. These support the OOPs concepts inheritance, abstraction etc.

Like Java, Kotlin also allows to create several objects of a class and you are free to include its class members and functions. We can control the visibility of the class members variables using different keywords that we will learn in Chapter 10 – Visibility Control. In the following example, we will create one class and its object through which we will access different data members of that class.

Sponsored
SyncSaS

SyncSaS Technologies is specialized in creating and designing customized software. We have our own team that will be in charge of developing your softwares.

Share
Published by

Recent Posts

Adsense Updated Dashboard

Changes in Adsense Dashboard Recently you might have seen few changes in Adsense dashboard, these…

3 weeks ago

Guide Book For Google Adsense

With my experience in working as AdSense publisher, I have written many articles, tutorials for…

3 weeks ago

How to manage Google Adsense to get High CPC

Manage Adsense for High CPC CPC (Cost per click) in simple words the CPC is…

3 weeks ago

Google Adsense Introduce page level ads

If you are already the publisher of Google Adsense account then now its time to…

3 weeks ago