Skip to content

Commit 4afec1b

Browse files
Merge pull request eugenp#5449 from kcacademic/kotlin-interfaces
Adding files for the article BAEL-2206
2 parents 45194b5 + bae7a74 commit 4afec1b

5 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.interfaces
2+
3+
interface BaseInterface {
4+
fun someMethod(): String
5+
}
6+
7+
interface FirstChildInterface : BaseInterface {
8+
override fun someMethod(): String {
9+
return("Hello, from someMethod in FirstChildInterface")
10+
}
11+
}
12+
13+
interface SecondChildInterface : BaseInterface {
14+
override fun someMethod(): String {
15+
return("Hello, from someMethod in SecondChildInterface")
16+
}
17+
}
18+
19+
class ChildClass : FirstChildInterface, SecondChildInterface {
20+
override fun someMethod(): String {
21+
return super<SecondChildInterface>.someMethod()
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.interfaces
2+
3+
interface MyInterface {
4+
fun someMethod(): String
5+
}
6+
7+
class MyClass() : MyInterface {
8+
override fun someMethod(): String {
9+
return("Hello, World!")
10+
}
11+
}
12+
13+
class MyDerivedClass(myInterface: MyInterface) : MyInterface by myInterface
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.interfaces
2+
3+
interface FirstInterface {
4+
fun someMethod(): String
5+
6+
fun anotherMethod(): String {
7+
return("Hello, from anotherMethod in FirstInterface")
8+
}
9+
}
10+
11+
interface SecondInterface {
12+
fun someMethod(): String {
13+
return("Hello, from someMethod in SecondInterface")
14+
}
15+
16+
fun anotherMethod(): String {
17+
return("Hello, from anotherMethod in SecondInterface")
18+
}
19+
}
20+
21+
class SomeClass: FirstInterface, SecondInterface {
22+
override fun someMethod(): String {
23+
return("Hello, from someMethod in SomeClass")
24+
}
25+
26+
override fun anotherMethod(): String {
27+
return("Hello, from anotherMethod in SomeClass")
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.interfaces
2+
3+
interface SimpleInterface {
4+
val firstProp: String
5+
val secondProp: String
6+
get() = "Second Property"
7+
fun firstMethod(): String
8+
fun secondMethod(): String {
9+
println("Hello, from: " + secondProp)
10+
return ""
11+
}
12+
}
13+
14+
class SimpleClass: SimpleInterface {
15+
override val firstProp: String = "First Property"
16+
override val secondProp: String
17+
get() = "Second Property, Overridden!"
18+
override fun firstMethod(): String {
19+
return("Hello, from: " + firstProp)
20+
}
21+
override fun secondMethod(): String {
22+
return("Hello, from: " + secondProp + firstProp)
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.interfaces
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
6+
class InterfaceExamplesUnitTest {
7+
@Test
8+
fun givenAnInterface_whenImplemented_thenBehavesAsOverridden() {
9+
val simpleClass = SimpleClass()
10+
assertEquals("Hello, from: First Property", simpleClass.firstMethod())
11+
assertEquals("Hello, from: Second Property, Overridden!First Property", simpleClass.secondMethod())
12+
}
13+
14+
@Test
15+
fun givenMultipleInterfaces_whenImplemented_thenBehavesAsOverridden() {
16+
val someClass = SomeClass()
17+
assertEquals("Hello, from someMethod in SomeClass", someClass.someMethod())
18+
assertEquals("Hello, from anotherMethod in SomeClass", someClass.anotherMethod())
19+
}
20+
21+
@Test
22+
fun givenConflictingInterfaces_whenImplemented_thenBehavesAsOverridden() {
23+
val childClass = ChildClass()
24+
assertEquals("Hello, from someMethod in SecondChildInterface", childClass.someMethod())
25+
}
26+
27+
@Test
28+
fun givenAnInterface_whenImplemented_thenBehavesAsDelegated() {
29+
val myClass = MyClass()
30+
assertEquals("Hello, World!", MyDerivedClass(myClass).someMethod())
31+
}
32+
}

0 commit comments

Comments
 (0)