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
2
3
4
public interface Hello{
String str = ”hello”;
void sayHello();
}
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
2
3
4
5
6
7
class HelloImplement implements Hello{
 
public void sayHello(){
System.out.println(“Hello”);
 }
 
}
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
2
3
Hello hello = new HelloImplement();
 
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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”);
}
 
}
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

Karibasappa G C (KB)
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