In this blog we will see different ways of implement interface in Java.
For example we have two interfaces, IntOne and IntTwo as below.
interface IntOne {public String getSrc();}interface IntTwo {public String getName();}
class IntOneImpl implements IntOne {private String src = "Source Interface One";public String getSrc() {return src;}}class IntTwoImpl implements IntTwo {private String name = "Java Interface Examples";public String getName(){return name;}}public class Main{public static void main(String[] args) {IntTwoImpl in = new IntTwoImpl();IntOneImpl one = new IntOneImpl();System.out.println(one.getSrc());System.out.println(in.getName());}}
Source Interface OneJava Interface Examples
class IntOneImpl implements IntOne {private String src = "Source Interface One";public String getSrc() {return src;}}class IntTwoImpl implements IntOne, IntTwo {private String name = "Java Interface Examples";IntOneImpl intOne = new IntOneImpl();@Overridepublic String getSrc() {return intOne.getSrc();}@Overridepublic String getName(){return name;}}public class Main{public static void main(String[] args) {IntTwoImpl in = new IntTwoImpl();System.out.println(in.getSrc());System.out.println(in.getName());}}
Lets see what is happeing in this code.
a. IntOneImpl implements the IntOne and provide definition of getSrc method.
b. IntTwoImpl implements IntOne and IntTwo interfaces and override methods from both the interfaces.