Hanbit the Developer
Kotlin Documentation | Inheritance 본문
Category: Concepts - Classes and objects
문서 링크: https://kotlinlang.org/docs/inheritance.html
Any
- 모든 클래스는 Any를 상속함
- equals()
- hashCode()
- toString()
Open
- open: 클래스를 상속하기 위해 사용한다.
open class Base(p: Int)
class Derived(p: Int) : Base(p)
open class Shape {
open fun draw() { /*...*/ }
fun fill() { /*...*/ }
}
class Circle() : Shape() {
override fun draw() { /*...*/ }
}
- final: override를 막고 싶을 때 사용한다.
open class Rectangle() : Shape() {
final override fun draw() { /*...*/ }
}
Constructor
class MyView : View {
constructor(ctx: Context) : super(ctx)
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}
Overriding properties
open class Shape {
open val vertexCount: Int = 0
}
class Rectangle : Shape() {
override val vertexCount = 4
}
interface Shape {
val vertexCount: Int
}
class Rectangle(override val vertexCount: Int = 4) : Shape // Always has 4 vertices
Derived class initialization order
open class Base(val name: String) {
init { println("Initializing a base class") }
open val size: Int =
name.length.also { println("Initializing size in the base class: $it") }
}
class Derived(
name: String,
val lastName: String,
) : Base(name.replaceFirstChar { it.uppercase() }.also { println("Argument for the base class: $it") }) {
init { println("Initializing a derived class") }
override val size: Int =
(super.size + lastName.length).also { println("Initializing size in the derived class: $it") }
}
Argument for the base class: Hello
Initializing a base class
Initializing size in the base class: 5
Initializing a derived class
Initializing size in the derived class: 10
→ 파생 클래스 프로퍼티를 베이스 클래스에서 사용 불가능
→ When designing a base class, you should therefore avoid using open members in the constructors, property initializers, or init blocks.
Superclass
open class Rectangle {
open fun draw() { println("Drawing a rectangle") }
val borderColor: String get() = "black"
}
class FilledRectangle : Rectangle() {
override fun draw() {
super.draw()
println("Filling the rectangle")
}
val fillColor: String get() = super.borderColor
}
class FilledRectangle: Rectangle() {
override fun draw() {
val filler = Filler()
filler.drawAndFill()
}
inner class Filler {
fun fill() { println("Filling") }
fun drawAndFill() {
super@FilledRectangle.draw() // Calls Rectangle's implementation of draw()
fill()
println("Drawn a filled rectangle with color ${super@FilledRectangle.borderColor}") // Uses Rectangle's implementation of borderColor's get()
}
}
}
Overriding rules
- super<ClassName> → 여러 클래스를 구현, 상속 시
open class Rectangle {
open fun draw() { /* ... */ }
}
interface Polygon {
fun draw() { /* ... */ } // interface members are 'open' by default
}
class Square() : Rectangle(), Polygon {
// The compiler requires draw() to be overridden:
override fun draw() {
super<Rectangle>.draw() // call to Rectangle.draw()
super<Polygon>.draw() // call to Polygon.draw()
}
}
'Kotlin' 카테고리의 다른 글
Kotlin Documentation | Interfaces (0) | 2023.05.22 |
---|---|
Kotlin Documentation | Properties (0) | 2023.05.22 |
Kotlin Documentation | Classes (0) | 2023.05.22 |
Kotlin Documentation | Reflection (0) | 2023.05.22 |
Kotlin Documentation | Destructuring declarations (1) | 2023.05.22 |