[Kotlin/Android] How to use abstract classes! Benefits and differences
What you will learn from this article
- What is abstract in Android Studio/Kotlin?
- Benefits and How to use
index
[open]
\ アプリをリリースしました /
Environment
- Android Studio:Meerkat
- Kotlin:2.0.21
What is abstract (abstract class)?
Official reference:abstract-classes
In Kotlin, you can define a class as an abstract class by adding abstract before class. An abstract class is a class that cannot be instantiated and is intended to be inherited by other classes. As the word "abstract" suggests, it is a vague, non-specific class.
An abstract class can define both abstract members (members that do not have a concrete implementation) and concrete members (members that do have a concrete implementation).
Uses of abstract
- If you want to define some implementation common to multiple classes
→ You can define some common methods and properties and make their implementation mandatory - If you want to pass a different class as a method argument, etc.
→ You can pass a class that inherits an abstract class
Differences from interface
The differences from interface, which has a similar role, are as follows.
| Characteristics | abstract | interface |
|---|---|---|
| Instantiability | Cannot | Cannot |
| Number of inheritances | Only one | Multiple possible |
| Properties | Abstract properties/concrete properties | Abstract properties only |
| Methods | Abstract methods/concrete methods | Abstract methods/concrete methods |
| Constructors | Can be implemented | Cannot be implemented |
How to use
To define an abstract class, add abstract. You can define properties and methods that you want to define as standards by specifying abstract/concrete.
abstract class Human {
// Abstract property
abstract var name: String
// Concrete property
var age: Int = 0
// Abstract method
abstract fun selfIntroduction()
// Concrete method
fun printName() {
print(name)
}
}
Abstract property (abstract var name: String)
name is defined as an abstract property by adding abstract. Abstract properties have no implementation, so they must be overridden in a subclass.
Concrete property (var age: Int = 0)
age is defined as a concrete property. Concrete properties can have initial values specified, so they do not need to be overridden in a subclass.
Abstract method (abstract fun selfIntroduction())
selfIntroduction is defined as an abstract method. Abstract methods have no implementation, so subclasses must override and implement them.
Concrete method (fun printName())
printName is defined as a concrete method. You can specify an implementation for a concrete method, and it is not necessary to override it in a subclass.
Inheritance to subclasses
To inheritance to subclasses, use Subclass name: Abstract class(). Anything defined as abstract members must be overridden and implemented when defining a subclass. A subclass can inherit only one abstract class.
class TargetPerson : Human() {
override var name: String = "ame"
override fun selfIntroduction() {
println("Hi! My Age is $age")
}
}
I am still learning, so if you find any mistakes or shortcomings, I would appreciate it if you could let me know.
Thank you for reading.






