[Kotlin/Android] How to use abstract classes! Benefits and differences

[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]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

Environment

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

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.

Search Box

Sponsor

ProFile

ame

Hobbies: reading, learning programming, website creation, blogging

I studied to get an IT pass in order to overcome my dislike of IT, and website creation became my hobby lol. Now I'm running this site completely by myself without using a CMS~

New Article

index