What is Interface and what power it gives to Java Inheritance


Interfaces in java, how the interface and abstract classes are different and what power interface gives to the Inheritance concept. What are main concepts related to interface.

Interface is nothing more than a feature of complete abstraction. Interfaces are so much more different than classes or abstract classes. The should purpose of Interface is to provide abstraction. You can not define the body of the methods in an interface, you only need to declare the signature of a method. Rest you put it up to the class which is implementing it to define the behavior of that method.

All the methods in an Interface are abstract and all the fields are by default final. Another point to be remember about Interfaces are that, the implementing class either must implement all the methods or the class must be abstract if it does not implement all the methods.

Please have a good look at program below and than we can talk further.


public interface TestInterface {
    public void echo(String s);
    public int sum(int a, int b);
    public int a = 100;
    /*public void test(){
        System.out.println("Hello World");
    }
        commented because interface abstract methods can not have body
    */
}

class Test implements TestInterface{
    Test(){
        //a = 200; it'll result in error on  removing the comment because it is by default final
    }
    
    public int sum(int a, int b){
        return a+b;
    }
    
    public void echo(String s){}
    
}

Now as you can see there are comments telling you why that part is comment out. Also I would suggest you to try and remove the method echo() from this program see if you are able to compile or otherwise what error you get.

Also try thi version of the program


abstract class Test implements TestInterface{
	public int sum(int a, int b){
		return a+b;
	}
}

now we have made this class abstract now try if this compiles with out the echo() method. Yes, it would. So now you see how it works. Java don't allow you to implement multiple inheritance with classes but it does allow you to implement multiple inheritance using Interfaces. Yes, you can implement more than on interface separated by ','. But before moving on let me tell you a few keywords of Java which are related to interfaces.


/*
interface - this keyword is used to define interface
*/
interface TestInterface{
	//interface definition
}

/*
implements - this keyword is used to inherit the interface into any class
*/
class Whatever implements TestInterface{
	//class definition
	//interface methods override
}

/*
if you can also extend an interface, but only in another interface.
*/
interface AnotherInterface extends Testinterface{
	//interface body definition
}

interface DemoInterface{
	//another demo interface
}

/*
you can also implement multiple interface in one class, separated by comma.
*/
class MultipleDemo implements DemoInterface, TestInterface{
	//class definition
	//DemoInterface methods implementation
	//TestInterface method implementation
}

So you see this is how you can play around with interfaces. One thing to remember is that Interfaces can't have its instances. But you can always use the references.

Note: interfaces and abstract class have so much in common but never confuse them to be same. There are lots of differences between these. Take it as an exercise and find out the differences, and when you do, post it here in comments or just let me know if you want them for you.