Adding files for the article BAEL-2206
This commit is contained in:
parent
289b94cfe0
commit
bae7a74acd
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.interfaces
|
||||
|
||||
interface BaseInterface {
|
||||
fun someMethod(): String
|
||||
}
|
||||
|
||||
interface FirstChildInterface : BaseInterface {
|
||||
override fun someMethod(): String {
|
||||
return("Hello, from someMethod in FirstChildInterface")
|
||||
}
|
||||
}
|
||||
|
||||
interface SecondChildInterface : BaseInterface {
|
||||
override fun someMethod(): String {
|
||||
return("Hello, from someMethod in SecondChildInterface")
|
||||
}
|
||||
}
|
||||
|
||||
class ChildClass : FirstChildInterface, SecondChildInterface {
|
||||
override fun someMethod(): String {
|
||||
return super<SecondChildInterface>.someMethod()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.interfaces
|
||||
|
||||
interface MyInterface {
|
||||
fun someMethod(): String
|
||||
}
|
||||
|
||||
class MyClass() : MyInterface {
|
||||
override fun someMethod(): String {
|
||||
return("Hello, World!")
|
||||
}
|
||||
}
|
||||
|
||||
class MyDerivedClass(myInterface: MyInterface) : MyInterface by myInterface
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.interfaces
|
||||
|
||||
interface FirstInterface {
|
||||
fun someMethod(): String
|
||||
|
||||
fun anotherMethod(): String {
|
||||
return("Hello, from anotherMethod in FirstInterface")
|
||||
}
|
||||
}
|
||||
|
||||
interface SecondInterface {
|
||||
fun someMethod(): String {
|
||||
return("Hello, from someMethod in SecondInterface")
|
||||
}
|
||||
|
||||
fun anotherMethod(): String {
|
||||
return("Hello, from anotherMethod in SecondInterface")
|
||||
}
|
||||
}
|
||||
|
||||
class SomeClass: FirstInterface, SecondInterface {
|
||||
override fun someMethod(): String {
|
||||
return("Hello, from someMethod in SomeClass")
|
||||
}
|
||||
|
||||
override fun anotherMethod(): String {
|
||||
return("Hello, from anotherMethod in SomeClass")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.interfaces
|
||||
|
||||
interface SimpleInterface {
|
||||
val firstProp: String
|
||||
val secondProp: String
|
||||
get() = "Second Property"
|
||||
fun firstMethod(): String
|
||||
fun secondMethod(): String {
|
||||
println("Hello, from: " + secondProp)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleClass: SimpleInterface {
|
||||
override val firstProp: String = "First Property"
|
||||
override val secondProp: String
|
||||
get() = "Second Property, Overridden!"
|
||||
override fun firstMethod(): String {
|
||||
return("Hello, from: " + firstProp)
|
||||
}
|
||||
override fun secondMethod(): String {
|
||||
return("Hello, from: " + secondProp + firstProp)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.interfaces
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class InterfaceExamplesUnitTest {
|
||||
@Test
|
||||
fun givenAnInterface_whenImplemented_thenBehavesAsOverridden() {
|
||||
val simpleClass = SimpleClass()
|
||||
assertEquals("Hello, from: First Property", simpleClass.firstMethod())
|
||||
assertEquals("Hello, from: Second Property, Overridden!First Property", simpleClass.secondMethod())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenMultipleInterfaces_whenImplemented_thenBehavesAsOverridden() {
|
||||
val someClass = SomeClass()
|
||||
assertEquals("Hello, from someMethod in SomeClass", someClass.someMethod())
|
||||
assertEquals("Hello, from anotherMethod in SomeClass", someClass.anotherMethod())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenConflictingInterfaces_whenImplemented_thenBehavesAsOverridden() {
|
||||
val childClass = ChildClass()
|
||||
assertEquals("Hello, from someMethod in SecondChildInterface", childClass.someMethod())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAnInterface_whenImplemented_thenBehavesAsDelegated() {
|
||||
val myClass = MyClass()
|
||||
assertEquals("Hello, World!", MyDerivedClass(myClass).someMethod())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue