Interface and its usage

Interface is a 100% abstract class. It means its method should not have its body part.
Signatures below:
User version

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.

The above interface declaration will be seen by compiler in below format:
Compiler version
Some key points for above demo:
1) Automatically “public abstract” will be prefixed to all available methods. Meaning “public abstract” is implicit while programming. Its up to the programmer to add to leave.
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.
The class who is going to use that interface has to use keyword “implements” as below:

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.

Now the question is, if programmer doesn’t want to override any method then what can be done? Is it possible? Yes its possible. Lets see how?

Some key points for above demo:
1)  The method which I did not want to override, I kept that with “public abstract method”. Other overridden methods of Interface has been prefixed with public access specifier in same way, what we did in earlier sample.
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.


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.

Let’s see, demo class which is reusing interfaces.

 Some key points for above demo:
1)  Class can’t extend more than one class at a time. But more than one interface can be implemented by class. Class “Demo” is implementing more than one interface.

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.

Constants in interface

Some key points for above demo:
1)  All constants will be implicitly “public static final” whether you add or not. If not added then automatically will be added by compiler.
2)  Private or protected not allowed.3)  Constants are final. So value can’t be modified at any
ways. Like COUNT = COUNT1 + COUNT2; is not possible

Interface can be passed to some method:

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.


Some key points for above demo:
1)  One file(.java) has only one public class. Another is default class. Public class named same as .java file name i.e,
2)  RunnableDemo class is implementing Runnable interface, so it must have to override run() method(callback).
3) Runnable interface has to be passed to Thread instance, in order to execute it.
If you like the post then please socialize it. Happy Sharing. Loudly Subscribing.

One Comment

  1. Pingback: Abstract Class - Technical Jungle

Leave a Reply

Your email address will not be published. Required fields are marked *