- abstract class which is used for achieving partial abstraction. Unlike abstract class an interface is used for full abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user
- Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract
- Also, the variables declared in an interface are public, static & final by default.
What is the use of interface in Java?
As mentioned above they are used for full abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.
Syntax:
Interfaces are declared by specifying a keyword “interface”. E.g.
interface MyInterface
{
/* All the methods are public abstract by default
* As you see they have no body
*/
public void method1();
public void method2();
}
Example of an Interface in Java
This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface or in other words you can say that class has to implement all the methods of interface.
Do you know? class implements interface but an interface extends another interface.
interface MyInterface
{
/* compiler will treat them as:
* public abstract void method1();
* public abstract void method2();
*/
public void method1();
public void method2();
}
class Demo implements MyInterface
{
/* This class must have to implement both the abstract methods
* else you will get compilation error
*/
public void method1()
{
System.out.println("implementation of method1");
}
public void method2()
{
System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new Demo();
obj.method1();
}
}
Output:
implementation of method1
Interface and Inheritance
an interface can not implement another interface. It has to extend the other interface. See the below example where we have two interfaces Inf1 and Inf2. Inf2 extends Inf1 so If class implements the Inf2 it has to provide implementation of all the methods of interfaces Inf2 as well as Inf1.
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1 {
public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the
* interface Inf2, it has to implement all the methods
* of Inf1 as well because the interface Inf2 extends Inf1
*/
public void method1(){
System.out.println("method1");
}
public void method2(){
System.out.println("method2");
}
public static void main(String args[]){
Inf2 obj = new Demo();
obj.method2();
}
}
In this program, the class Demo only implements interface Inf2, however it has to provide the implementation of all the methods of interface Inf1 as well, because interface Inf2 extends Inf1.
Key points: Here are the key points to remember about interfaces:
1) We can’t instantiate an interface in java. That means we cannot create the object of an interface
2) Interface provides full abstraction as none of its methods have body. On the other hand abstract class provides partial abstraction as it can have abstract and concrete(methods with body) methods both.
3) implements keyword is used by classes to implement an interface.
4) While providing implementation in class of any method of an interface, it needs to be mentioned as public.
5) Class that implements any interface must implement all the methods of that interface, else the class should be declared abstract.
6) Interface cannot be declared as private, protected or transient.
7) All the interface methods are by default abstract and public.
8) Variables declared in interface are public, static and final by default.
interface Try
{
int a=10;
public int a=10;
public static final int a=10;
final int a=10;
static int a=0;
}
All of the above statements are identical.
9) Interface variables must be initialized at the time of declaration otherwise compiler will throw an error.
interface Try
{
int x;//Compile-time error
}
Above code will throw a compile time error as the value of the variable x is not initialized at the time of declaration.
10) Inside any implementation class, you cannot change the variables declared in interface because by default, they are public, static and final. Here we are implementing the interface “Try” which has a variable x. When we tried to set the value for variable x we got compilation error as the variable x is public static final by default and final variables can not be re-initialized.
class Sample implements Try
{
public static void main(String args[])
{
x=20; //compile time error
}
}
11) An interface can extend any interface but cannot implement it. Class implements interface and interface extends interface.
12) A class can implement any number of interfaces.
13) If there are two or more same methods in two interfaces and a class implements both interfaces, implementation of the method once is enough.
interface A
{
public void aaa();
}
interface B
{
public void aaa();
}
class Central implements A,B
{
public void aaa()
{
//Any Code here
}
public static void main(String args[])
{
//Statements
}
}
14) A class cannot implement two interfaces that have methods with same name but different return type.
interface A
{
public void aaa();
}
interface B
{
public int aaa();
}
class Central implements A,B
{
public void aaa() // error
{
}
public int aaa() // error
{
}
public static void main(String args[])
{
}
}
15) Variable names conflicts can be resolved by interface name.
interface A
{
int x=10;
}
interface B
{
int x=100;
}
class Hello implements A,B
{
public static void Main(String args[])
{
/* reference to x is ambiguous both variables are x
* so we are using interface name to resolve the
* variable
*/
System.out.println(x);
System.out.println(A.x);
System.out.println(B.x);
}
}
Advantages of interface in java:
Advantages of using interfaces are as follows:
- Without bothering about the implementation part, we can achieve the security of implementation
- In java, multiple inheritance is not allowed, however you can use interface to make use of it as you can implement more than one interface.
Difference Between Abstract Class and Interface in Java
Abstract Class | Interface | |
1 | An abstract class can extend only one class or one abstract class at a time | An interface can extend any number of interfaces at a time |
2 | An abstract class can extend another concrete (regular) class or abstract class | An interface can only extend another interface |
3 | An abstract class can have both abstract and concrete methods | An interface can have only abstract methods |
4 | In abstract class keyword “abstract” is mandatory to declare a method as an abstract | In an interface keyword “abstract” is optional to declare a method as an abstract |
5 | An abstract class can have protected and public abstract methods | An interface can have only have public abstract methods |
6 | An abstract class can have static, final or static final variable with any access specifier | interface can only have public static final (constant) variable |
Difference No.1: Abstract class can extend only one class or one abstract class at a time
class Example1{
public void display1(){
System.out.println("display1 method");
}
}
abstract class Example2{
public void display2(){
System.out.println("display2 method");
}
}
abstract class Example3 extends Example1{
abstract void display3();
}
class Example4 extends Example3{
public void display3(){
System.out.println("display3 method");
}
}
class Demo{
public static void main(String args[]){
Example4 obj=new Example4();
obj.display3();
}
}
Output:
display3 method
Interface can extend any number of interfaces at a time
//first interface
interface Example1{
public void display1();
}
//second interface
interface Example2 {
public void display2();
}
//This interface is extending both the above interfaces
interface Example3 extends Example1,Example2{
}
class Example4 implements Example3{
public void display1(){
System.out.println("display2 method");
}
public void display2(){
System.out.println("display3 method");
}
}
class Demo{
public static void main(String args[]){
Example4 obj=new Example4();
obj.display1();
}
}
Output:
display2 method
Difference No.2: Abstract class can be extended(inherited) by a class or an abstract class
class Example1{
public void display1(){
System.out.println("display1 method");
}
}
abstract class Example2{
public void display2(){
System.out.println("display2 method");
}
}
abstract class Example3 extends Example2{
abstract void display3();
}
class Example4 extends Example3{
public void display2(){
System.out.println("Example4-display2 method");
}
public void display3(){
System.out.println("display3 method");
}
}
class Demo{
public static void main(String args[]){
Example4 obj=new Example4();
obj.display2();
}
}
Output:
Example4-display2 method
Interfaces can be extended only by interfaces. Classes has to implement them instead of extend
interface Example1{
public void display1();
}
interface Example2 extends Example1{
}
class Example3 implements Example2{
public void display1(){
System.out.println("display1 method");
}
}
class Demo{
public static void main(String args[]){
Example3 obj=new Example3();
obj.display1();
}
}
Output:
display1 method
Difference No.3: Abstract class can have both abstract and concrete methods
abstract class Example1 {
abstract void display1();
public void display2(){
System.out.println("display2 method");
}
}
class Example2 extends Example1{
public void display1(){
System.out.println("display1 method");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}
Interface can only have abstract methods, they cannot have concrete methods
interface Example1{
public abstract void display1();
}
class Example2 implements Example1{
public void display1(){
System.out.println("display1 method");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}
Output:
display1 method
Difference No.4: In abstract class, the keyword ‘abstract’ is mandatory to declare a method as an abstract
abstract class Example1{
public abstract void display1();
}
class Example2 extends Example1{
public void display1(){
System.out.println("display1 method");
}
public void display2(){
System.out.println("display2 method");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}
In interfaces, the keyword ‘abstract’ is optional to declare a method as an abstract because all the methods are abstract by default
interface Example1{
public void display1();
}
class Example2 implements Example1{
public void display1(){
System.out.println("display1 method");
}
public void display2(){
System.out.println("display2 method");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}
Difference No.5: Abstract class can have protected and public abstract methods
abstract class Example1{
protected abstract void display1();
public abstract void display2();
public abstract void display3();
}
class Example2 extends Example1{
public void display1(){
System.out.println("display1 method");
}
public void display2(){
System.out.println("display2 method");
}
public void display3(){
System.out.println("display3 method");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}
Interface can have only public abstract methods
interface Example1{
void display1();
}
class Example2 implements Example1{
public void display1(){
System.out.println("display1 method");
}
public void display2(){
System.out.println("display2 method");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}
Difference No.6: Abstract class can have static, final or static final variables with any access specifier
abstract class Example1{
private int numOne=10;
protected final int numTwo=20;
public static final int numThree=500;
public void display1(){
System.out.println("Num1="+numOne);
}
}
class Example2 extends Example1{
public void display2(){
System.out.println("Num2="+numTwo);
System.out.println("Num2="+numThree);
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
obj.display2();
}
}
Interface can have only public static final (constant) variable
interface Example1{
int numOne=10;
}
class Example2 implements Example1{
public void display1(){
System.out.println("Num1="+numOne);
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display1();
}
}