Interface and its usage
Signatures below:
1 2 3 4 5 |
interface InterfaceDemo { int onSuccess(); int onFail(); } |
Some key points for above demo:
1) Keyword “interface” used here for declaring it.
2) No method body. No curly braces like {…}.
3) Interface can either by default or public specifier.
4) It can’t be private or protected.
5) At the end of the method scope it is been terminated with semi colon i,e. ‘;’. It doesn’t have braces {…} which is used to implement the method.
1 2 3 4 5 |
abstract interface InterfaceDemo { public abstract void onSuccess(); public abstract void onFail(); } |
Some key points for above demo:
2) So if programmer is using compiler version also then it’s ok. Both versions are fine for development.3) Interface will be prefixed with “abstract” by compiler.
4) Private or protected not allowed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Demo implements InterfaceDemo{ @Override public void onSuccess() { // TODO Auto-generated method stub } @Override public void onFail() { // TODO Auto-generated method stub } //Extra method added public void display(){ System.out.println("Ritesh is demonstrating about interface."); } // Main method to run this program. public static void main(String []args){ Demo demo = new Demo(); demo.display(); } } |
Some key points for above demo:
1) Keyword “implements” used here.
2) Both methods of Interface has been overridden.
3) All overridden methods has to prefixed with public access specifier and exact signature used in interface. Its must. Without ‘public’ access specifier it will give compile time error.
4) Both methods has method body as {….}
5) Extra methods can be added.
6) Interface methods can’t be static.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
abstract class Demo implements InterfaceDemo{ @Override public void onSuccess() { // TODO Auto-generated method stub } public abstract void onFail(); public void display(){ System.out.println("Ritesh is demonstrating about interface."); } } |
Some key points for above demo:
2) Since method is “abstract”, hence class also has to be marker as “abstract”.3) Instance (object) can’t be created for abstract class.
4) Means no main method in required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
abstract class Demo implements InterfaceDemo{ @Override public void onSuccess() { // TODO Auto-generated method stub } public abstract void onFail(); public void display(){ System.out.println("Ritesh is demonstrating about interface."); } } //One interface can inherit existing interface by “extends” keyword as below. interface InterfaceDemo2 extends InterfaceDemo{ void onSuccess(); void onError(); } |
Or
“public abstract” method itself can be removed as below. But remember class will be remain “abstract” only. It’s must.
Let’s consider one more interface, which will be implemented in demo class.
1 2 3 4 |
interface InterfaceDemo3 { public abstract void onResponse(); } |
Let’s see, demo class which is reusing interfaces.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class Demo implements InterfaceDemo, InterfaceDemo3 { @Override public void onSuccess() { // TODO Auto-generated method stub } // public abstract void onFail(); public void display() { System.out.println("Ritesh is demonstrating about interface."); } @Override public void onResponse() { // TODO Auto-generated method stub } @Override public void onFail() { // TODO Auto-generated method stub } // Extra method added public void display() { System.out.println("Ritesh is demonstrating about interface."); } // Main method to run this program. public static void main(String[] args) { Demo demo = new Demo(); demo.display(); } } Output : Ritesh is demonstrating about interface. |
Some key points for above demo:
2) Comma will be used for separating more than one interfaces.3) If two or more interfaces will have just one copy then only one copy will be overridden. The final copy.
1 2 3 4 5 6 7 8 9 10 11 12 |
interface InterfaceDemo { public abstract void onSuccess(); public abstract void onFail(); int COUNT1 = 1; // possible final int COUNT2 = 2; // possible static final int COUNT3 = 3; // possible public static final int COUNT4 = 4; // possible public final int COUNT7 = 7; // possible private static final int COUNT5 = 5; // Not possible protected static final int COUNT6 = 6; // Not possible } |
Some key points for above demo:
ways. Like COUNT = COUNT1 + COUNT2; is not possible
Interface can be passed to some method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Demo { // Extra method added public void display(InterfaceDemo3 demo3) { demo3.onResponse(); } // Main method to run this program. public static void main(String[] args) { Demo demo = new Demo(); demo.display(new InterfaceDemo3() { @Override public void onResponse() { System.out.println("Ritesh onResponse."); } }); } } Output: Ritesh onResponse. |
Some key points for above demo:
1) Interface can be pass as reference.
2) Interface can be packed in method and pass. From called method, interface’s callbacks can be invoked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class DemoThread { // Main method to run this program. public static void main(String[] args) { RunnableDemo demo = new RunnableDemo(); Thread thread = new Thread(demo); /*'start' used to run the spawn & start new thread.*/ thread.start(); } } class RunnableDemo implements Runnable{ @Override public void run() { System.out.println("www.TechnicalJungle.com -> thread executed"); } } Output: www.TechnicalJungle.com -> thread executed |
Pingback: Abstract Class - Technical Jungle