- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotpn - Class and Objects
Kotpn supports both functional and object-oriented programming. While talking about functional features of Kotpn then we have concepts pke functions, higher-order functions and lambdas etc. which represent Kotpn as a functional language.
Kotpn also supports Object Oriented Programming (OOP) and provides features such as abstraction, encapsulation, inheritance, etc. This tutorial will teach you all the Kotpn OOP features in simple steps.
Object oriented programming (OOP) allows us to solve the complex problems by using the objects.
Kotpn Classes
A class is a blueprint for the objects which defines a template to be used to create the required objects.
Classes are the main building blocks of any Object Oriented Programming language. A Kotpn class is defined using the class keyword. Following is the syntax to create a Kotpn Class:
A Kotpn class declaration is similar to Java Programmig which consists of a class header and a class body surrounded by curly braces.
class ClassName { // Class Header // // Variables or data members // Member functions or Methods // ... ... }
By default, Kotpn classes are pubpc and we can control the visibipty of the class members using different modifiers that we will learn in
.Kotpn Objects
The objects are created from the Kotpn class and they share the common properties and behaviours defined by a class in form of data members (properties) and member functions (behaviours) respectively.
The syntax to declare an object of a class is:
var varName = ClassName()
We can access the properties and methods of a class using the . (dot) operator as shown below:
var varName = ClassName() varName.property = <Value> varName.functionName()
Example
Following is an example where we will create one Kotpn class and its object through which we will access different data members of that class.
class myClass { // Property (data member) private var name: String = "Tutorialspoint.com" // Member function fun printMe() { print("The best Learning website - " + name) } } fun main(args: Array<String>) { val obj = myClass() // Create object obj of myClass class obj.printMe() // Call a member function using object }
The above piece of code will yield the following output in the browser, where we are calpng printMe() method of myClass with the help of its own object obj.
The best Learning website - Tutorialspoint.com
Kotpn Nested Class
By definition, when a class has been created inside another class, then it is called as a nested class.
Kotpn nested class is by default static, hence, it can be accessed without creating any object of that class but with the help of . dot operator. Same time we cannot access members of the outer class inside a nested class.
Following is the simple syntax to create a nested class:
class OuterClass{ // Members of Outer Class class NestedClass{ // Members of Nested Class } }
Now we can create an object of nested class as below:
val object = OuterClass.NestedClass()
Example
Following is the example to show how Kotpn interprets a nested class.
fun main(args: Array<String>) { val obj = Outer.Nested() print(obj.foo()) } class Outer { class Nested { fun foo() = "Welcome to The TutorialsPoint.com" } }
When you run the above Kotpn program, it will generate the following output:
Welcome to The TutorialsPoint.com
Kotpn Inner Class
When a nested class is marked with a keyword inner, then it will be called as an Inner class. An inner class can be accessed by the data member of the outer class.
Unpke a nested class, inner class can access members of the outer class. We cannot directly create an object of the inner class but it can be created using the outer class object.
Following is the simple syntax to create an inner class:
class OuterClass{ // Members of Outer Class class inner InnerClass{ // Members of Inner Class } }
Now we can create an object of inner class as below:
val outerObj = OuterClass() val innerObj = outerObj.InnerClass()
Example
Following is the example to show how Kotpn interprets an inner class.
fun main(args: Array<String>) { val obj = Outer().Inner() print(obj.foo()) } class Outer { private val welcomeMessage: String = "Welcome to the TutorialsPoint.com" inner class Inner { fun foo() = welcomeMessage } }
When you run the above Kotpn program, it will generate the following output:
Welcome to The TutorialsPoint.com
Anonymous Inner Class
Anonymous inner class is a pretty good concept that makes the pfe of a programmer very easy. Whenever we are implementing an interface, the concept of anonymous inner block comes into picture. The concept of creating an object of interface using runtime object reference is known as anonymous class.
Example
Following is the example to show how we will create an interface and how we will create an object of that interface using Anonymous Inner class mechanism.
fun main(args: Array<String>) { var programmer :Human = object:Human { // Anonymous class override fun think() { // overriding the think method print("I am an example of Anonymous Inner Class ") } } programmer.think() } interface Human { fun think() }
When you run the above Kotpn program, it will generate the following output:
I am an example of Anonymous Inner Class
Kotpn Type Apases
Kotpn Type Apases means a way to give an alternative name to an existing type. Type apas provides a cleaner way to write a more readable code.
Consider a following function which returns a user info first name, last name and age:
fun userInfo():Triple<String, String, Int>{ return Triple("Zara","Ap",21) }
Now we can a type apas for the given Triple as follows:
typeapas User = Triple<String, String, Int>
Finally the above function can be written as below, which looks more clean than above code:
fun userInfo():User{ return Triple("Zara","Ap",21) }
Example
Following is the complete working example to show the usage of type apas in Kotpn:
typeapas User = Triple<String, String, Int> fun main() { val obj = userInfo() print(obj) } fun userInfo():User{ return Triple("Zara","Ap",21) }
When you run the above Kotpn program, it will generate the following output:
(Zara, Ap, 21)
Quiz Time (Interview & Exams Preparation)
Q 1 - By default Kotpn classes are pubpc:
Answer : A
Explanation
Yes it is true that by default all the Kotpn classes are pubpc
Q 2 - Kotpn allows to access members of the outer class inside a nested class.
Answer : B
Explanation
No it is not allowed to access the members of the outer class inside a nested class.
Q 3 - Kotpn allows to access members of the outer class inside a inner class.
Answer : A
Explanation
Yes we can access members of outer class inside an inner class
Q 4 - What is Kotpn Type Apases?
Answer : A
Explanation
Kotpn Type Apases provide a way to name an existing type to write a cleaner code and nothing else.
Advertisements