2
2
.
.
4
4
.
.
5
5
I
I
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
s
s
I
I
n
n
f
f
o
o
Interface is collection of Methods & Properties that can be used or implemented by the Class.
This ensures that all classes that implement the same interface have the same set of functionality (interface).
Class can implement multiple interfaces using column ":" followed by the comma separated list of Interfaces.
Class that implements Interface
must override Abstract methods (use abstract Keyword and don't provide implementation)
can override Default methods (just provide implementation)
Interfaces were introduced to avoid Multiple inheritance problem which occurs when Class can extend multiple Classes.
So in Java Class can only extend one Class but it can implement multiple interfaces.
M
M
e
e
t
t
h
h
o
o
d
d
s
s
Abstract method
can't have implementation defined inside Interface (Interface implementation is forbidden)
must be overridden in each Class that Implements Interface (Class implementation is mandatory)
Default method
must have implementation defined inside Interface (Interface implementation is mandatory)
don't need to be overridden in the Class that Implements Interface (Class implementation is optional)
S
S
y
y
n
n
t
t
a
a
x
x
Interface is declared by
using Keyword interface interface
followed by Interface Name InterfaceA
followed by Interface Body which can have
Methods abstract void abstractMethod() (abstract, static, default)
Properties abstract void abstractMethod() (abstract, static, default)
Syntax
interface InterfaceA { }
interface InterfaceB { }
class Person : InterfaceA, InterfaceB { }
Syntax
//===========================================================================================================
//INTERFACE: InterfaceA
//===========================================================================================================
interface InterfaceA {
abstract fun abstractMethod();
}
//===========================================================================================================
//INTERFACE: InterfaceB
//===========================================================================================================
interface InterfaceB {
fun defaultMethod1() { println("defaultMethod1()"); }
fun defaultMethod2() { println("defaultMethod2()"); }
}
//===========================================================================================================
//CLASS: Person
//===========================================================================================================
class Person : InterfaceA, InterfaceB {
override fun abstractMethod() { println("Overriden abstractMethod()" ); }
override fun defaultMethod1() { println("Overriden defaultMethod1()" ); }
constructor() {
}
}
//===========================================================================================================
//FUNCTION: main
//===========================================================================================================
fun main() {
//CREATE INSTANCE.
var john : Person = Person()
john.abstractMethod()
john.defaultMethod1() //Call overriden Method
john.defaultMethod2() //Call Parent's Method
}