Interfaces in Java

Interfaces

It is one of the ways to achieve abstraction in Java.

It will have only method declaration(abstract methods) and constant attributes in it.

It cannot be instantiated like how we can’t instantiate abstract class.

They are used to achieve multiple inheritance and polymorphism.

Java interface Example

  1. public interface Hello{
  2. String str = ”hello”;
  3. void sayHello();
  4. }
public interface Hello{
String str = ”hello”;
void sayHello();
}

Note : All the variables inside Interface are public , static and final even if we don’t specify anything.

All methods are public and abstract even if we don’t specify anything.

Also we can’t change these default access modifiers.

variables of Interfaces in Java

Since all variables inside interface are static, we can access it directly using interface name.

Ex :

  1. System.out.println(Hello.str);
System.out.println(Hello.str);

And these variables are public, we can access it anywhere in the application.

Also these variables are final, so we can’t modify them.

Methods of Interfaces in Java

Since all the methods inside interface are abstract, they must be overridden in the implementing class.

And since methods are public, they must be accessible anywhere in the application using object of subclass.

Why can’t we access methods using interface name ?

They are not static methods, so we need object to access them.

let’s implement this interface

  1. class HelloImplement implements Hello{
  2.  
  3. public void sayHello(){
  4. System.out.println(“Hello”);
  5.  }
  6.  
  7. }
class HelloImplement implements Hello{

public void sayHello(){
System.out.println(“Hello”);
 }

}

Now the class HelloImplement has implemented the interface Hello.

So now we can access this method using an object of this class as below

  1. Hello hello = new HelloImplement();
  2.  
  3. hello.sayHello();
Hello hello = new HelloImplement();

hello.sayHello();

Notice the point here that , the class instance(new HelloImplement()) is assigned to interface variable. This is possible as Interface is parent and this is called upcasting. This we can discuss in detail while discussing Casting.

Class uses implements keyword to inherit the interface.

Interface uses extends keyword to inherit the interface.

Let’s see how class can achieve multiple Inheritance through interfaces in Java

  1. interface A{
  2.  
  3. void show();
  4.  
  5. }
  6.  
  7. interface B{
  8.  
  9. void print();
  10.  
  11. }
  12.  
  13. class C implements A,B{
  14.  
  15. public void show(){
  16. System.out.println(“show”);
  17. }
  18.  
  19. public void print(){
  20. System.out.println(“show”);
  21. }
  22.  
  23. }
interface A{

void show();

}

interface B{

void print();

}

class C implements A,B{

public void show(){
System.out.println(“show”);
}

public void print(){
System.out.println(“show”);
}

}

Why multiple Inheritance is allowed in Java through interface ?

Reason is very simple, when a class implements ‘n’ interfaces and if all ‘n’ interfaces has same method with same signature also, for an implementer class
There will be no ambiguity as class has to provide the definition forcefully.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on