

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create an instance of an abstract class in Kotlin?
When a class is defined in Kotlin with an abstract keyword, then it is known as an abstract class. In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature. In order to use an abstract class, we need to create another class and inherit the abstract class.
Example – Abstract class in Kotlin
The following example demonstrates how you can create an instance of an abstract class in Kotlin.
abstract class myInter { abstract var absVariable : String abstract fun absMethod() } class myClass : myInter() { // we cannot create an instance of abstract class; // however we can implement the same functionality using another class override var absVariable: String = "Test" override fun absMethod() { println("Implemented the abstract method") } } fun main(args: Array<String>) { var obj = myClass() obj.absMethod() }
Output
On execution, it will produce the following output −
Implemented the abstract method
- Related Questions & Answers
- How to create an instance of an anonymous interface in Kotlin?
- Can we create an object of an abstract class in Java?
- Instance child class in abstract static method PHP?
- How to get the class name of an instance in Python?
- How to check if an object is an instance of a Class in JavaScript?
- Can we create an object for the abstract class in java?
- Can we define an abstract class without abstract method in java?
- Can we define an abstract class with no abstract methods in Java?
- How to create an empty array in Kotlin?
- How can we create an instance of VarHandle in Java 9?
- When to use an abstract class and when to use an interface in Java?
- What is the difference between an interface and an abstract class in C#?
- How to create an immutable class in Java?
- Create new instance of an Array with Java Reflection Method
- How to implement an instance method reference using a class name in Java?
Advertisements