Site icon C1CTech

Kotlin Interfaces(With Examples)

&NewLine;<p>This article is about interfaces and how to implement it in Kotlin with the help of simple examples&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Interfaces<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In Kotlin&comma; Interfaces can contain declarations of abstract methods &lpar;have no body&rpar;&comma; as well as method implementation of non-abstract methods&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Interfaces can have properties but these need to be abstract or to provide accessor implementations&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Creating Interface<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>To declare an interface&comma; we use <strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">interface<&sol;span><&sol;strong> keyword&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;By default interfaces and <&sol;span><&sol;strong>&NewLine;&sol;&sol;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>abstract members&lpar;properties and methods&rpar;<&sol;strong> <&sol;span><&sol;strong>&NewLine;&sol;&sol;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">are public abstract<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">public abstract<&sol;span><&sol;strong> <strong>interface <&sol;strong>MusicPlayer &lbrace;&NewLine; <strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">public abstract<&sol;span><&sol;strong> <strong>var defaultVolume<&sol;strong>&colon; String <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; abstract property<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">public abstract<&sol;span> fun <&sol;strong>play&lpar;&rpar; <span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong> <em>&sol;&sol; abstract function<&sol;em><&sol;strong><&sol;span><em>&NewLine; <&sol;em><strong>fun <&sol;strong>stop&lpar;&rpar; &equals; <strong>"the MusicPlayer is in stop mode" <&sol;strong><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; function with default implementation<&sol;span><&sol;strong>&NewLine;<&sol;em>&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Here&comma; <strong>MusicPlayer<&sol;strong> is an interface which contains a property <strong>defaultVolume<&sol;strong> &comma; abstract function <strong>play&lpar;&rpar;<&sol;strong> and non-abstract function <strong>stop&lpar;&rpar;<&sol;strong> &period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Note&colon;<&sol;span><&sol;strong> Interfaces and Interface members &lpar;abstract and non-abstract&rpar; are always open&period; You do not need to explicitly use <strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">open<&sol;span><&sol;strong> keyword&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Implementing an Interface<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">Interfaces cannot be instantiated because it contains abstract members &lpar;have no implementation&rpar;&period; But&comma; we can implement interfaces in other classes&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The class which implements Interface must <strong><span style&equals;"color&colon; &num;008000&semi;">override<&sol;span><&sol;strong> all abstract members &lpar;abstract properties and abstract methods&rpar; of Interface or you have to declare class as <span style&equals;"color&colon; &num;008000&semi;"><strong>abstract<&sol;strong><&sol;span> class&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example<&sol;span><&sol;strong> <&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Implementing an interface example<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>interface <&sol;strong>MusicPlayer &lbrace;&NewLine;&NewLine; <strong>fun <&sol;strong>play&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract function<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>fun <&sol;strong>stop&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract function<&sol;span><&sol;strong>&NewLine;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;non-abstract function with default implementation<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>fun <&sol;strong>setVolume&lpar;x&colon; Int&rpar; &lbrace;&NewLine;&NewLine; System&period;<em>out<&sol;em>&period;println&lpar;<strong>"Player volume Changed by&colon; " <&sol;strong>&plus; x&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;MP3Player class implementing MusicPlayer interface&period;&NewLine;&sol;&sol;It must override all the abstract functions of MusicPlayer interface&period;<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>MP3Player &colon; MusicPlayer &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of play abstract function<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>play&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"the MP3 Player is playing&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of stop abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>stop&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"the MP3 Player is off&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of setVolume <em><strong>function<&sol;strong><&sol;em> <&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>setVolume&lpar;x&colon; Int&rpar; &lbrace;&NewLine;&NewLine; <strong>if <&sol;strong>&lpar;x &lt&semi;&equals; 100&rpar; &lbrace;&NewLine; System&period;<em>out<&sol;em>&period;println&lpar;<strong>"Player volume Changed by&colon; " <&sol;strong>&plus; x&rpar;&semi;&NewLine; &rcub; <strong>else <&sol;strong>&lbrace;&NewLine; System&period;<em>out<&sol;em>&period;println&lpar;<strong>"Player volume level should be less than 100&period;"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>fun <&sol;strong>main&lpar;&rpar; &lbrace;&NewLine; <strong>val <&sol;strong>mP3Player &equals; MP3Player&lpar;&rpar;&NewLine; mP3Player&period;play&lpar;&rpar;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;setVolume <em><strong>function<&sol;strong><&sol;em> of MP3Player class get executed<&sol;span><&sol;strong>&NewLine; <&sol;em>mP3Player&period;setVolume&lpar;110&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">the MP3 Player is playing&period;&NewLine;Player volume level should be less than 100&period;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example explained<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list" id&equals;"block-69491672-b201-486f-ace5-5fddb5ee3629"><li><strong>MusicPlayer<&sol;strong> is an interface which contains a non-abstract function setVolume&lpar;&rpar; and two abstract functions play&lpar;&rpar; and stop&lpar;&rpar;&period;<&sol;li><li>The class <strong>MP3Player<&sol;strong> implements interface <strong>MusicPlayer<&sol;strong> and overrides its functions &lpar;abstract and non-abstract functions&rpar;&period; <&sol;li><li><strong>mP3Player<&sol;strong> is an object of MP3Player class&period; The statement <strong>mP3Player&period;play&lpar;&rpar;<&sol;strong> will execute the play function and <strong>mP3Player&period;setVolume&lpar;&rpar;<&sol;strong> will execute the setVolume function of MP3Player class&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Note&colon;<&sol;span><&sol;strong> If a class implementing the Interface does not override a non-abstract function of Interface&comma; then the compiler will execute the default implementation of that function within the Interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color">Properties in Interfaces<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>You can declare properties in interfaces&period; A property declared in an interface can either be <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">abstract<&sol;span><&sol;strong>&comma; or it can <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">provide implementations for accessors<&sol;span><&sol;strong>&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Properties declared in interfaces can&&num;8217&semi;t have <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">backing fields<&sol;span><&sol;strong>&comma; and therefore accessors declared in interfaces can&&num;8217&semi;t reference them&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><strong><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example<&sol;span><&sol;strong><&sol;strong><&sol;strong> <&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;interface<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>interface <&sol;strong>Addition &lbrace;&NewLine;&NewLine; <strong>val numOne<&sol;strong>&colon; Int <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; abstract property<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>val numTwo<&sol;strong>&colon; Int <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;property with implementation<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>get<&sol;strong>&lpar;&rpar; &equals; 6&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;function with default implementation<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>fun <&sol;strong>add&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Sum of &dollar;numOne and &dollar;numTwo is&colon; " <&sol;strong>&plus; &lpar;<strong>numOne <&sol;strong>&plus; <strong>numTwo<&sol;strong>&rpar;&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<strong>class <&sol;strong>Number &colon; Addition &lbrace;&NewLine;&NewLine; <strong>override val numOne <&sol;strong>&equals; 5&NewLine;&NewLine; override <strong>fun <&sol;strong>add&lpar;&rpar; &lbrace;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;add <em><strong>function<&sol;strong><&sol;em> of Addition Interface get executed<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>super<&sol;strong>&period;add&lpar;&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>fun <&sol;strong>main&lpar;&rpar; &lbrace;&NewLine; <strong>val <&sol;strong>number &equals; Number&lpar;&rpar;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;add <em><strong>function<&sol;strong><&sol;em> of Number class get executed<&sol;span><&sol;strong>&NewLine; <&sol;em>number&period;add&lpar;&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Sum of 5 and 6 is&colon; 11<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color">Interface reference type<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>We cannot create an object of any interface but Interfaces can be used as <strong>reference type<&sol;strong> for the object of an implementing class&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Using interface reference type we can only access its own functions and properties&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example<&sol;span><&sol;strong> <&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Interface<&sol;span><&sol;strong> &NewLine;<&sol;em><strong>interface <&sol;strong>MusicPlayer &lbrace;&NewLine;&NewLine; <strong>fun <&sol;strong>play&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>fun <&sol;strong>stop&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;&NewLine;<&sol;em>&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;MP3Player class implementing MusicPlayer interface&period;<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>MP3Player &colon; MusicPlayer &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of play abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>play&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"the MP3 Player is playing&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of stop abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>stop&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"the MP3 Player is in stop mode&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;MP3Player class non-abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>fun <&sol;strong>pause&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"the MP3 Player is in pause mode&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>fun <&sol;strong>main&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Using interface reference type<&sol;span><&sol;strong> &NewLine; <strong>val <&sol;strong>mP3Player&colon; <strong>MusicPlayer<&sol;strong> &equals; MP3Player&lpar;&rpar;&NewLine; mP3Player&period;play&lpar;&rpar;&NewLine; mP3Player&period;stop&lpar;&rpar;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;using interface reference type we can only access its own members&period;&NewLine; <&sol;strong><&sol;span><strong><span class&equals;"has-inline-color has-black-color">&sol;&sol;mP3Player&period;pause&lpar;&rpar;<&sol;span><&sol;strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong> &sol;&sol;error&colon; interface MusicPlayer does not contain any pause <em><strong>function<&sol;strong><&sol;em><&sol;strong><&sol;span>&NewLine;<&sol;em>&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">the MP3 Player is playing&period;<br>the MP3 Player is in stop mode&period;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Multiple inheritance by interface<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Kotlin does not support multiple inheritance which means that a class cannot extend more than one class&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">But&comma; multiple inheritance using interfaces is possible&period; We can implement multiple Interfaces in a single class&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">A class implementing Interfaces must implement all the abstract methods of all the Interfaces&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example<&sol;span><&sol;strong> <&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Interface<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>interface <&sol;strong>MusicPlayer &lbrace;&NewLine;&NewLine; <strong>fun <&sol;strong>play&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>fun <&sol;strong>stop&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;<&sol;em>&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Interface<&sol;span><&sol;strong><&sol;em>&NewLine;<strong>interface <&sol;strong>VideoPlayer &lbrace;&NewLine;&NewLine; <strong>fun <&sol;strong>changeBrightness&lpar;x&colon; Int&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>fun <&sol;strong>changeContrast&lpar;x&colon; Int&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;<&sol;em>&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;SmartPhone class implementing interfaces MusicPlayer&comma;VideoPlayer<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>SmartPhone &colon; MusicPlayer&comma; VideoPlayer &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of play abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>play&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Player is playing&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of stop abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>stop&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Player stopped&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of changeBrightness abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>changeBrightness&lpar;x&colon; Int&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Brightness Changed by&colon; " <&sol;strong>&plus; x&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of changeContrast abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>changeContrast&lpar;x&colon; Int&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Contrast Changed by&colon; " <&sol;strong>&plus; x&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>fun <&sol;strong>main&lpar;&rpar; &lbrace;&NewLine; <strong>val <&sol;strong>smartPhone &equals; SmartPhone&lpar;&rpar;&NewLine; smartPhone&period;play&lpar;&rpar;&NewLine; smartPhone&period;changeBrightness&lpar;30&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Player is playing&period;<br>Brightness Changed by&colon; 30<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Resolving overriding conflicts &lpar;multiple Interfaces having same method&rpar;<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In case both the implemented interfaces contain non-abstract method &lpar;let&&num;8217&semi;s say<strong>&nbsp&semi;<code>next&lpar;&rpar;<&sol;code>&nbsp&semi;<&sol;strong>method&rpar; with same method signature&comma; the implementing class must override the next&lpar;&rpar; method and this method must explicitly specify which next&lpar;&rpar; method is to be used&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example<&sol;span><&sol;strong> <&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Interface<&sol;span><&sol;strong>&NewLine;<strong>interface <&sol;strong>MusicPlayer &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"> <strong>&sol;&sol;fun play&lpar;&rpar;&NewLine;&NewLine; &sol;&sol;fun stop&lpar;&rpar;<&sol;strong><&sol;span>&NewLine;<em>&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;next <em><strong><em><strong>function<&sol;strong><&sol;em><&sol;strong><&sol;em> of MusicPlayer Interface<&sol;span><&sol;strong><&sol;em>&NewLine;&sol;&sol;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">with default implementation<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>fun <&sol;strong>next&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Next from MusicPlayer"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Interface<&sol;span><&sol;strong>&NewLine;<strong>interface <&sol;strong>VideoPlayer &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;fun changeBrightness&lpar;x&colon; Int&rpar;&NewLine;&NewLine; &sol;&sol;fun changeContrast&lpar;x&colon; Int&rpar;<&sol;span><&sol;strong>&NewLine;<em><strong>&NewLine;<&sol;strong><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;next <em><strong><em><strong>function<&sol;strong><&sol;em><&sol;strong><&sol;em> of VideoPlayer Interface<&sol;span><&sol;strong><&sol;em><&sol;em>&NewLine;&sol;&sol;<em><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">with default implementation<&sol;span><&sol;strong><&sol;em>&NewLine; <&sol;em><strong>fun <&sol;strong>next&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Next from VideoPlayer"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;SmartPhone class implementing interfaces MusicPlayer&comma;VideoPlayer<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>SmartPhone &colon; VideoPlayer&comma; MusicPlayer &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;overriding next&lpar;&rpar; <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>next&lpar;&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;use super keyword to call the next&NewLine; &sol;&sol;<em><strong>function<&sol;strong><&sol;em> of MusicPlayer interface<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>super<&sol;strong>&lt&semi;MusicPlayer&gt&semi;&period;next&lpar;&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;use super keyword to call the next&NewLine; &sol;&sol;<em><strong>function<&sol;strong><&sol;em> of VideoPlayer interface<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>super<&sol;strong>&lt&semi;VideoPlayer&gt&semi;&period;next&lpar;&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>fun <&sol;strong>main&lpar;&rpar; &lbrace;&NewLine; <strong>val <&sol;strong>smartPhone &equals; SmartPhone&lpar;&rpar;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"> <em>&sol;&sol;next <em><strong>function<&sol;strong><&sol;em> of SmartPhone class get executed<&sol;em><&sol;span><&sol;strong><em>&NewLine; <&sol;em>smartPhone&period;next&lpar;&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Next from MusicPlayer<br>Next from VideoPlayer<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Inheriting an Interface<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>An interface can inherit&nbsp&semi;another interface or interfaces &lpar;more than one interface&rpar; &period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Any class that implements an interface must implement the abstract members declared in that interface plus all the <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">abstract members<&sol;span><&sol;strong> that are present in the super interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>If the implementing class is <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">abstract<&sol;span><&sol;strong> it may choose to implement all&comma; some or none of the methods declared in the interface&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Example<&sol;span><&sol;strong> <&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Interfaces inheritance example<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>interface <&sol;strong>Player &lbrace;&NewLine;&NewLine; <strong>fun <&sol;strong>play&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>fun <&sol;strong>stop&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;<&sol;em>&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;VideoPlayer interface inheriting Player interface<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>interface <&sol;strong>VideoPlayer &colon; Player &lbrace;&NewLine;&NewLine; <strong>fun <&sol;strong>changeBrightness&lpar;x&colon; Int&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;function with default implementation<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>fun <&sol;strong>next&lpar;&rpar; &lbrace;&NewLine; System&period;<em>out<&sol;em>&period;println&lpar;<strong>"Next from VideoPlayer"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;SmartPhone class implementing VideoPlayer interface<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>class <&sol;strong>SmartPhone &colon; VideoPlayer &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of play abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>play&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"video playing&period;&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of stop abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>stop&lpar;&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"video stopped&period;&period;"<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;implementation of changeBrightness abstract <em><strong>function<&sol;strong><&sol;em><&sol;span><&sol;strong>&NewLine; <&sol;em><strong>override fun <&sol;strong>changeBrightness&lpar;x&colon; Int&rpar; &lbrace;&NewLine; <em>println<&sol;em>&lpar;<strong>"Brightness Changed by&colon; " <&sol;strong>&plus; x&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>fun <&sol;strong>main&lpar;&rpar; &lbrace;&NewLine; <strong>val <&sol;strong>smartPhone &equals; SmartPhone&lpar;&rpar;&NewLine; smartPhone&period;play&lpar;&rpar;&NewLine; smartPhone&period;changeBrightness&lpar;30&rpar;&NewLine; smartPhone&period;next&lpar;&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Output<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">video playing&period;&period;<br>Brightness Changed by&colon; 30<br>Next from VideoPlayer<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Points to Remember&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list" id&equals;"block-c1b41eaa-6411-4180-bade-8bbefbce6d18"><li>Interfaces can contain declarations of <span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>abstract methods<&sol;strong> <&sol;span>&lpar;have no body&rpar;&comma; as well as method implementations&period;&nbsp&semi;<&sol;li><li>Interface also contains properties which can either be abstract&comma; or it can provide implementations for accessors&period;<&sol;li><li>Interfaces cannot be instantiated&period; They can only be implemented by an implementing class or extended by another interface&period;<&sol;li><li>Interfaces can be used as <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">reference type<&sol;span><&sol;strong> for the object of an implementing class&period;<&sol;li><li>A class that implements interface must provide implementation to all the abstract methods of interface or you have to declare it as <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">abstract<&sol;span><&sol;strong> class&period;<&sol;li><li>A class can implement more than one interface&period;<&sol;li><li>An interface can <strong>inherit<&sol;strong> more than one interface&period;<&sol;li><li>Kotlin does not support multiple inheritance using class&comma; but by using <strong>interface<&sol;strong> it can achieve multiple inheritance&period;<&sol;li><li>Interface is used to achieve abstraction and loose coupling&period;<&sol;li><&sol;ul>&NewLine;&NewLine;

Exit mobile version