Site icon C1CTech

Java Interfaces(with Examples)

&NewLine;<p class&equals;"p1">This article is about interfaces and how to implement it in Java with the help of examples&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Interface<&sol;b><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">An interface in java is a collection of fields and abstract methods <span style&equals;"color&colon; &num;000000&semi;">&lpar;only method signature&comma; no body&rpar;<&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>Interfaces are by default prefix with <span style&equals;"color&colon; &num;008000&semi;"><strong>abstract <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">keyword<&sol;span><strong>&comma; <&sol;strong><&sol;span>which means similar to abstract classes we cannot create an object of interface&period;<&sol;li><li class&equals;"p2">All methods in an interface are by default <span style&equals;"color&colon; &num;008000&semi;"><strong>public<&sol;strong><&sol;span> <strong><span style&equals;"color&colon; &num;008000&semi;">abstract <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">&lpar;only method signature&comma; no body&rpar;&period;<&sol;span><&sol;li><li class&equals;"p2">All properties &lpar;data fields&rpar; in an interface are <strong><span style&equals;"color&colon; &num;008000&semi;">public<&sol;span><&sol;strong> <span style&equals;"color&colon; &num;008000&semi;"><strong>static final <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and <&sol;span><&sol;span>must be initialized at the time of declaration&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Creating Interface<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>To declare an interface&comma; we use <span style&equals;"color&colon; &num;0000ff&semi;"><strong>interface<&sol;strong><&sol;span> keyword&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;by default interface members are <&sol;span>public&NewLine;<&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">abstract <strong><span style&equals;"color&colon; &num;0000ff&semi;">interface<&sol;span><&sol;strong><&sol;span> MusicPlayer&lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;declare constant fields are<&sol;strong> <strong>public static final<&sol;strong><&sol;span>&NewLine; <span style&equals;"color&colon; &num;000000&semi;">public static final <&sol;span>int defaultVolume &equals; 30&semi;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;declare methods are<&sol;strong> <strong>public abstract&lpar;no body&rpar;<&sol;strong><&sol;span>&NewLine; <span style&equals;"color&colon; &num;000000&semi;">public abstract <&sol;span>void play&lpar;&rpar;&semi;&NewLine; <span style&equals;"color&colon; &num;000000&semi;">public abstract <&sol;span>void stop&lpar;&rpar;&semi;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Here&comma; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MusicPlayer<&sol;strong><&sol;span> is an interface which contains a field <span style&equals;"color&colon; &num;008000&semi;"><strong>defaultVolume<&sol;strong><&sol;span> and two abstract methods&lpar; play&lpar;&rpar; and stop&lpar;&rpar; &rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Implementing an Interface<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">Interfaces cannot be instantiated because it contains abstract methods &lpar;have no body&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 methods of Interface or you have to declare it as <span style&equals;"color&colon; &num;008000&semi;"><strong>abstract<&sol;strong><&sol;span> class&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>In Java&comma; we use the <strong><span style&equals;"color&colon; &num;0000ff&semi;">implements<&sol;span><&sol;strong>&nbsp&semi;keyword to implement interfaces&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Implementing an interface example&period;<&sol;strong><&sol;span>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">interface<&sol;span><&sol;strong> MusicPlayer &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;play&lpar;&rpar; and stop&lpar;&rpar; are abstract methods<&sol;strong><&sol;span>&NewLine; public <span style&equals;"color&colon; &num;0000ff&semi;"><strong>abstract<&sol;strong><&sol;span> void play&lpar;&rpar;&semi;&NewLine;&NewLine; public <strong><span style&equals;"color&colon; &num;0000ff&semi;">abstract<&sol;span><&sol;strong> void stop&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;MP3Player class implementing MusicPlayer interface&period;&NewLine;&sol;&sol;It must override all the abstract methods of MusicPlayer interface&period;<&sol;span><&sol;strong>&NewLine;class MP3Player <span style&equals;"color&colon; &num;0000ff&semi;"><strong>implements<&sol;strong><&sol;span> MusicPlayer &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;implementation of play abstract method <&sol;strong><&sol;span>&NewLine; public void play&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is playing&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of stop abstract method<&sol;span> <&sol;strong>&NewLine; public void stop&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is off&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; MP3Player mp3Player &equals; new MP3Player&lpar;&rpar;&semi;&NewLine; mp3Player&period;play&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">the MP3 Player is playing&period;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MusicPlayer<&sol;strong><&sol;span> is an interface which contains two abstract methods play&lpar;&rpar; and stop&lpar;&rpar;&period;<&sol;li><li class&equals;"p1">To create an object of <span style&equals;"color&colon; &num;008000&semi;"><strong>MP3Player<&sol;strong><&sol;span>&comma; we must provide an implementation for the play&lpar;&rpar; and stop&lpar;&rpar; methods inside MP3Player class&period;<&sol;li><li class&equals;"p1">Now&comma; by creating an object of MP3Player we can call the methods of Interface &lpar;play&lpar;&rpar; and stop&lpar;&rpar;&rpar;&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Interface reference variable<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>We cannot create an object of any interface but we can create reference variable of interface that refers to the object of its implementing class&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Using interface reference variable we can only access its own methods and variables&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Interface reference variable example&period;<&sol;span><&sol;strong>&NewLine;interface MusicPlayer &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;play&lpar;&rpar; and stop&lpar;&rpar; are abstract methods<&sol;strong><&sol;span>&NewLine; void play&lpar;&rpar;&semi;&NewLine;&NewLine; void stop&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;MP3Player class implementing MusicPlayer interface<&sol;span><&sol;strong>&NewLine;class MP3Player implements MusicPlayer &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;MP3Player class method<&sol;strong><&sol;span>&NewLine; public void pause&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is in pause state"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of play abstract method<&sol;span> <&sol;strong>&NewLine; public void play&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is playing&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of stop abstract method<&sol;span> <&sol;strong>&NewLine; public void stop&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is off&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; MusicPlayer musicPlayer &equals; new MP3Player&lpar;&rpar;&semi;&NewLine;&NewLine; musicPlayer&period;play&lpar;&rpar;&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;the MP3 Player is playing&period;&NewLine;&NewLine; &sol;&sol;error&colon; MusicPlayer does not contain pause&lpar;&rpar; method&period; <&sol;span><&sol;strong>&NewLine; <strong> &sol;&sol;musicPlayer&period;pause&lpar;&rpar;&semi;<&sol;strong>&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"h2 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Multiple inheritance by interface<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Java does not support multiple inheritance which means that a class cannot extend more than one class&period;<&sol;p>&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"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Multiple inheritance by interface example&period;<&sol;strong><&sol;span>&NewLine;interface MusicPlayer &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;play&lpar;&rpar; and stop&lpar;&rpar; are abstract methods<&sol;strong><&sol;span>&NewLine; void play&lpar;&rpar;&semi;&NewLine;&NewLine; void stop&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;interface VideoPlayer &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;changeContrast&lpar;&rpar; and changeBrightness&lpar;&rpar; are abstract methods<&sol;strong><&sol;span>&NewLine; void changeContrast&lpar;int x&rpar;&semi;&NewLine;&NewLine; void changeBrightness&lpar;int x&rpar;&semi;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Smartphone class implementing interfaces<&sol;span> <span style&equals;"color&colon; &num;008000&semi;">MusicPlayer&comma;VideoPlayer<&sol;span><&sol;strong>&NewLine;class Smartphone implements MusicPlayer&comma; VideoPlayer &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of play abstract method<&sol;span> <&sol;strong>&NewLine; public void play&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"playing&period;&period;&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of stop abstract method<&sol;span> <&sol;strong>&NewLine; public void stop&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"stopped&period;&period;&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of changeContrast abstract method<&sol;span> <&sol;strong>&NewLine; public void changeContrast&lpar;int x&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Constrast Changed by&colon; " &plus; x&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of changeBrightness abstract method<&sol;span> <&sol;strong>&NewLine; public void changeBrightness&lpar;int x&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Brightness Changed by&colon; " &plus; x&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; Smartphone smartphone &equals; new Smartphone&lpar;&rpar;&semi;&NewLine; smartphone&period;play&lpar;&rpar;&semi;&NewLine; smartphone&period;changeBrightness&lpar;20&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">playing&period;&period;&period;&NewLine;Brightness Changed by&colon; 20<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading" id&equals;"inheriting-an-interface">&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Interface Default methods<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Before Java 8&comma; interface contains only abstract methods&period; The implementation of these methods has to be provided in a class which implements the interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">So&comma; if a new method is to be added in an interface&comma; then its implementation code has to be provided in all the classes implementing the same interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">To overcome this issue&comma; Java 8 now provides us <span style&equals;"color&colon; &num;0000ff&semi;"><strong>default methods<&sol;strong> <&sol;span>for Interfaces&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;008000&semi;">Default methods<&sol;span><&sol;strong> allow the interfaces to have methods with implementation without affecting the classes that implement the interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>If a class does not implement a default method&comma; the compiler will take the implementation mentioned within the Interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">To declare default methods inside interfaces&comma; we use the&nbsp&semi;<strong><span style&equals;"color&colon; &num;0000ff&semi;">default<&sol;span><&sol;strong>&nbsp&semi;keyword&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;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Interface default methods example<&sol;strong><&sol;span>&NewLine;interface MusicPlayer &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;play&lpar;&rpar; and stop&lpar;&rpar; are abstract methods<&sol;span><&sol;strong>&NewLine; void play&lpar;&rpar;&semi;&NewLine;&NewLine; void stop&lpar;&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default method<&sol;span><&sol;strong>&NewLine; default void setVolume&lpar;int x&rpar; &lbrace;&NewLine;&NewLine; System&period;out&period;println&lpar;"Player volume Changed by&colon; " &plus; x&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;MP3Player class implementing MusicPlayer interface<&sol;span><&sol;strong>&NewLine;class MP3Player implements MusicPlayer &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of play abstract method<&sol;span> <&sol;strong>&NewLine; public void play&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is playing&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of stop abstract method<&sol;span> <&sol;strong>&NewLine; public void stop&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is off&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of setVolume default method<&sol;span> <&sol;strong>&NewLine; public void setVolume&lpar;int x&rpar; &lbrace;&NewLine; if &lpar;x &lt&semi;&equals; 100&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Player volume Changed by&colon; " &plus; x&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; System&period;out&period;println&lpar;"Player volume level should be less then 100&period;"&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; MP3Player mp3Player &equals; new MP3Player&lpar;&rpar;&semi;&NewLine; mp3Player&period;play&lpar;&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;setVolume default method defined in MP3Player class get executed<&sol;span><&sol;strong>&NewLine; mp3Player&period;setVolume&lpar;110&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&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 then 100&period;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Default Methods and Multiple Inheritance<&sol;b><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">In case both the implemented interfaces contain default methods with same method signature&comma; the implementing class must override the default method and must explicitly specify which default method is to be used&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;multiple inheritance through default methods example&period;<&sol;strong><&sol;span>&NewLine;interface MusicPlayer &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default method<&sol;span><&sol;strong>&NewLine; default void next&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Next from MusicPlayer"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;interface VideoPlayer &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;default method<&sol;span><&sol;strong>&NewLine; default void next&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Next from VideoPlayer"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Smartphone class implementing MusicPlayer and VideoPlayer interface<&sol;span><&sol;strong>&NewLine;class Smartphone implements MusicPlayer&comma; VideoPlayer &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;overriding default next&lpar;&rpar; method<&sol;span><&sol;strong>&NewLine; public void next&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;use super keyword to call the next <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;method of MusicPlayer interface<&sol;span><&sol;strong>&NewLine; MusicPlayer&period;super&period;next&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;use super keyword to call the next <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;method of VideoPlayer interface<&sol;span><&sol;strong>&NewLine; VideoPlayer&period;super&period;next&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; Smartphone smartphone &equals; new Smartphone&lpar;&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default next&lpar;&rpar; method executed<&sol;span><&sol;strong>&NewLine; smartphone&period;next&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Next from MusicPlayer&NewLine;Next from VideoPlayer<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Interface static methods<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Java 8 also provides the ability to add static methods to interfaces&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Static methods in interfaces are almost identical to static methods in normal classes&period; The only big difference is that static methods are not inherited in the classes that implements the interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">This means that we can access static methods of an interface using its references only&nbsp&semi;and not by the object of a class which implements the interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Interface static method example<&sol;span><&sol;strong>&NewLine;interface MusicPlayer &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;static method<&sol;span><&sol;strong>&NewLine; public static void print&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"MusicPlayer static method called&period;&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;abstract method<&sol;span><&sol;strong>&NewLine; void play&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;MP3Player class implementing MusicPlayer interface<&sol;span><&sol;strong>&NewLine;class MP3Player implements MusicPlayer &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of play abstract method<&sol;span> <&sol;strong>&NewLine; public void play&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"the MP3 Player is playing&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; MP3Player mp3Player &equals; new MP3Player&lpar;&rpar;&semi;&NewLine; &NewLine; MusicPlayer&period;print&lpar;&rpar;&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; static method executed<&sol;span><&sol;strong>&NewLine; <strong>&sol;&sol; mp3Player&period;print&lpar;&rpar;&semi;<&sol;strong> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; This will cause a compilation error&NewLine; &rcub;<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">MusicPlayer static method called&period;&period;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading" id&equals;"inheriting-an-interface">&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading" id&equals;"inheriting-an-interface"><span style&equals;"color&colon; &num;000080&semi;"><strong>Inheriting an Interface<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">An interface can <span style&equals;"color&colon; &num;000000&semi;">extend<&sol;span> 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 methods declared in that interface plus all the <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">abstract methods<&sol;span><&sol;strong> that are present in the super interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">In Java&comma;&nbsp&semi; <strong><span style&equals;"color&colon; &num;0000ff&semi;">extends<&sol;span><&sol;strong>&nbsp&semi;keyword is used for extending interfaces&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Inheriting an interface example&period;<&sol;span><&sol;strong>&NewLine;interface Player &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;abstract methods play&lpar;&rpar; and stop&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; public void play&lpar;&rpar;&semi;&NewLine;&NewLine; public void stop&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;VideoPlayer extending Player interface<&sol;strong><&sol;span>&NewLine;interface VideoPlayer extends Player &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;abstract method<&sol;span><&sol;strong>&NewLine; void changeBrightness&lpar;int x&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;default method<&sol;span><&sol;strong>&NewLine; default public void next&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Next from VideoPlayer"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;class must implement abstract methods of VideoPlayer and Player<&sol;span><&sol;strong>&NewLine;class SmartPhone implements VideoPlayer &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of play abstract method<&sol;span> <&sol;strong>&NewLine; public void play&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"video playing&period;&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of stop abstract method<&sol;span> <&sol;strong>&NewLine; public void stop&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"video stopped&period;&period;"&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;implementation of changeBrightness abstract method<&sol;span> <&sol;strong>&NewLine; public void changeBrightness&lpar;int x&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Brightness Changed by&colon; " &plus; x&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class Main &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; SmartPhone smartphone &equals; new SmartPhone&lpar;&rpar;&semi;&NewLine; smartphone&period;play&lpar;&rpar;&semi;&NewLine; smartphone&period;changeBrightness&lpar;20&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">video playing&period;&period;&NewLine;Brightness Changed by&colon; 20<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading">&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><b>Points to Remember&colon;<&sol;b><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<ol class&equals;"ol1 wp-block-list"><li>Interface contains <span style&equals;"color&colon; &num;0000ff&semi;"><b>methods <&sol;b><&sol;span><span style&equals;"color&colon; &num;008000&semi;"><strong>&lpar;implicitly public abstract&rpar;<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><b>fields <&sol;b><&sol;span><span style&equals;"color&colon; &num;008000&semi;"><strong>&lpar;implicitly public static final&rpar;<&sol;strong><&sol;span><b>&period;<&sol;b><&sol;li><li>In Java 8 and above&comma; Interfaces can have <strong><span style&equals;"color&colon; &num;008000&semi;">default methods<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">static methods<&sol;span><&sol;strong>&period;<&sol;li><li>An Interface cannot be instantiated but we can create <strong><span style&equals;"color&colon; &num;008000&semi;">reference variable<&sol;span> <&sol;strong>of it that refers to the object of its implemented 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;0000ff&semi;">abstract<&sol;span><&sol;strong> class&period;<&sol;li><li>Interface do not have <strong><span style&equals;"color&colon; &num;008000&semi;">constructors<&sol;span><&sol;strong>&period;<&sol;li><li>To inherit an interface within a class we need to use <strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">implements<&sol;span><&sol;strong> keyword and to inherit an interface in another interface we need to use <strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">extends<&sol;span><&sol;strong> keyword&period;<&sol;li><li>A class can <span style&equals;"color&colon; rgb&lpar;0&comma; 128&comma; 0&rpar;&semi;"><strong>implement<&sol;strong><&sol;span> more than one interface&period;<&sol;li><li>An interface can <strong><span style&equals;"color&colon; &num;008000&semi;">extends<&sol;span><&sol;strong> more than one interface&period;<&sol;li><li>Java does not support multiple inheritance using class&comma; but by using <strong><span style&equals;"color&colon; &num;008000&semi;">interface<&sol;span><&sol;strong> it is possible&period;<&sol;li><li>Interface is used to achieve abstraction and loose coupling&period;<&sol;li><&sol;ol>&NewLine;&NewLine;

Exit mobile version