OBJECT ORIENTED PROGRAMMING WITH KOTLIN I


 

OBJECT ORIENTED PROGRAMMING WITH KOTLIN I

Hello and welcome back to our Kotlin series. In this post we now want to focus on Object oriented programming with KOTLIN.

Object-oriented programming is a programming paradigm based on the concept of "objects", which can contain data and code. Objects are instantiated from classes but as we shall see in Kotlin just like JavaScript we don’t need classes to create objects, we can use the object keyword to make an object. In Kotlin, getters and setters are optional and are auto-generated if you do not create them in your program.

In this article we cover Kotlin Class and Objects, Kotlin Constructors, Kotlin Visibility Modifiers, Kotlin Abstract Classes and Interfaces and Kotlin Inheritance.

Classes and Object

Kotlin does actually supports both functional and object-oriented programming. Language features such as higher-order functions, function types and lambdas which makes it a great choice for working in functional programming style.  First let’s talk about object-oriented programming. And this is built on the idea of object which are instances of classes. Classes are Blueprints for creating Objects. In object-oriented style of programming, you can divide a complex problem into smaller sets by creating objects. These objects share two characteristics which are state and behavior. We can create many objects from a class.

 Classes in Kotlin unlike Java are by default PUBLIC


Kotlin Constructors

A constructor is used initialize class properties. It is a special member function that is called when an object is instantiated (created). In Kotlin, there are two constructors:  The Primary constructor which is used to initialize a class and Secondary constructors which allows you to put additional initialization code.

The primary constructor is part of the class header.

In Kotlin, a class can also contain one or more secondary constructors. They are created using constructor keyword.  Secondary constructors are not that common in Kotlin. The most common use of secondary constructor comes up when you need to extend a class that provides multiple constructors that initialize the class in different ways


Kotlin Visibility Modifiers

Visibility modifiers are keywords that set the accessibility of classes, objects, interface, constructors and even functions.

Visibility Modifiers Inside Packages

Modifier

Use Case

public

declarations are visible everywhere in the concerted project

private           

visible inside the file or class where it’s been declared

internal

visible inside the same package (a set grouped of Kotlin files)

protected

not available for packages (used for subclasses)


Kotlin Abstract Classes and Interfaces

Both Abstract classes and Interfaces enable Inheritance.

Abstract class cannot be instantiated meaning we cannot create objects of an abstract class. However, subclasses can inherit from them. The abstract keyword is used to declare abstract classes in Kotlin.

The properties and methods of an abstract class are considered non-abstract. We would need to use the abstract keyword to make them abstract. Abstract classes are always open, meaning we can inherit from them without declaring them open

.

Interfaces in Kotlin just like in Java can contain declarations of abstract methods and method implementations. What makes them different from abstract classes is that interfaces cannot store state. They can have properties but these need to be abstract or to provide accessor implementations. Interfaces are used to provide the benefits of multiple inheritance. Several classes can share a set of methods and constants without requiring these methods and constants to be implemented by a common superclass.

An interface is defined using the keyword interface


 Kotlin Inheritance

Inheritance is one of the key features of object-oriented programming. It is so important in programming as it allows code reusability. It allows user to create a new class, the derived class that uses functionality and properties from an existing class or base class.  The derived class inherits all the features from the base class and can have additional features of its own.

By default, all classes in Kotlin are final. Therefore, to allow a class to be inherited, we need to attach the open modifier before the class to make it non-final.  Also, to allow Properties and Functions to be overridden we need to set the open modifier on them too.


That’s it for this article Stay safe Happy Coding and remember nerd's brain is the sexiest thing on this planet.








Comments

Post a Comment

Popular Posts