추상 클래스 : 아직 구현되지 않은 메서드가 일부 있는 클래스.
open
→ 상속으로 구체화된 기능을 구현함abstract class Person {
fun displayAge(age: Int) {
println("I'm $age years old.")
}
abstract fun introOneself (intro: String) //추상 메서드
}
abstract class Person(name: String) {
var age: Int = 0
init {
println("My name is $name")
}
fun displayAge(age: Int) {
println("I'm $age years old.")
}
abstract fun introOneSelf(intro: String)
}
class EnglishMan(name: String): Person(name) {
override fun introOneSelf(intro: String) {
println("Hello. and I'm from England. ${intro}")
}
}
fun main(args: Array<String>) {
val jane = EnglishMan("Jane")
jane.displayAge(11)
jane.introOneSelf("I like football.")
}
interface A {
fun A(){
println("A")
}
}
interface B {
fun B() {
println("B")
}
}
class Child: A, B
fun main(args: Array<String>) {
val obj = Child()
obj.A()
obj.B()
2개의 인터페이스가 동일하다면 문제가 발생함.
Cannot infer visibility for 'callMe'. Please specify it explicitly.
Class 'Child' must override 'callMe' because it inherits multiple interface methods for it.
interface A {
**fun callMe() {
println("From interface A")
}**
}
interface B {
**fun callMe() {
println("From interface B")
}
}**
class Child: A, B
fun main(args: Array<String>) {
val obj = Child()
obj.callMe()
}
해결방법 : 오버라이드
class C: A, B {
override fun callMe() {
super<A>.callMe()
super<B>.callMe()
}
}
fun main(args: Array<String>) {
val obj = C()
obj.callMe()
}