An interface is a rule repository or coding contract. As in class, Interface also has methods and variables, but the interface does not have a constructor. All the methods in the interface are abstract by default.
Basically, in the interface, we write all the common behavior(method) which expected to perform by a particular category of objects.
Let us try to understand with Real World example.
Real World Example of Interface in Java
There are wide varieties of Animals live around us such as dogs, cows, cats, etc. and they all belong to a single category that is Animals. And these animals have different ways of making sounds, like the sound of dogs always be different than cats and cows.
But they all have common behavior that is making a Sound. Now if we look with java’s point of view, we can create an Interface named Animals and put common behaviors or methods under that.
In this case, makeSound() is a method, but I will not add any logic inside the method because each animal has a different way of making a sound, so I cannot hard code here.
Therefore, I will create a separate class for each animal as shown in the figure, and all these class will implements the interface so that the makeSound() method will inherit to every class, and under the class, we will define logic or implementation based on the animal, As shown in the figure.
Real time Example of Interface in Java
An interface in java is also defined as the intermediate between the consumers and services.
JDBC is an interface between the java program and the database.
JDBC is Java Database Connectivity, which is used to establish a proper communication of java program with database. It basically acts as an intermediate between the java program and database.
So that you can easily play around with the data present in the database with the help of a line of code written in Java. JDBC provides a smooth communication with data present in the database.
In the above example, java program is consumer and database is a service, we are using the services of the database in our java program with the help of an intermediate called Interface.
Here the meaning of communication refers to any of the following things.
- With the help of a line of code written in java, you fetch the data present in the database.
- You can update the data present in the database.
- Easily insert the data from the client to the database.
- And many SQL Operations you can perform.
The whole point of explaining the above is to give a clear idea about how JDBC is acting as an intermediate between programs and database..
Important points regarding Interface
- Programatically an interface is the one which has only an abstract method.
- Since it has only an abstract method, so we also called Interface as a 100% abstract class.
- All methods are abstract, so with the help of Interface, we can achieve Abstraction.
- All the members such as variables or methods present inside the Interface have public access modifiers only.
Note: Data Members that present inside the Interface by default it is public static and final.
In the above point, if you noticed, there is a line, with the help of Interface we can achieve the abstraction. Yes, it is absolutely correct.
In the JDBC example, I mentioned many times that it helps to establish the communication between java program and database, but do you know how JDBC established this connection? You don’t know. Right?
The thing is that there are so many methods that are present in JDBC jar files, which help to perform all types of data related operations between programs and databases.
But all these methods are not visible to us as users, these methods are defined in classes and interface, but as a user, we cannot see it. This is only called Abstraction.
So, Abstraction in java is a process of hiding the internal details from the users and showing the necessary functionality.
Explanation of public, static, and final data members in Interface.
- public: Data members can be accessed from anywhere, such as within the class or outside the class or within the packages or outside the packages.
- static: Data members can be accessed with the help of Class name only, no need to create an object. Here we use the name of the class which implemented the interface.
- final: It means once you assigned the value to data members at the time of declaration, then the value will never be modified or changes.
Why the interface does not contain any Constructor?
Since all data members are static and final, it means they all are class variable or I can say they are not an instance or object variable because of static keyword. And if you remember the whole purpose of a constructor is to initialize the instance variable of the class.
Hence, no instance variables are available in Interface, so there is no meaning of constructor presence inside Interface. Thus, the interface does not contain any Constructor.
Implementation of the interface in Java
The interface can be implemented with the help of Inheritance in Java. The class can inherit the Interface which the help of implements keyword. And the class which implements the interface is called Implementation Class.
Interface vs Abstract class
Interface | abstract class |
---|---|
The member of Interface can only have public access modifiers. | Abstract Class members can have any access modifiers such as public, private, etc. |
An interface can have only an abstract method. | It can have both concrete as well as an abstract method. |
The data members of Interface is always final. | Abstract Class can have final or non-final data members. |
The interface does not have a constructor. | Abstract Class has Constructor. |
Multiple Inheritance is possible in Interface. | In Abstract Class it is not possible. |
The interface can be inherited by using keyword implements. | Abstract Class can inherit by using the keyword extends. The member of Interface can only have public access modifiers whereas in Abstract Class members can have any access modifiers such as public, private, etc. |
Why multiple Inheritance is possible in Interface but not in Class?
Multiple Inheritance is not possible in Java Class because of two reasons:
- Ambiguity while constructor chaining.
- Ambiguity while method execution.
Ambiguity while constructor chaining.
Assuming that a class extends more than one superclasses, then the subclass constructor call superclass constructor using super() and there are more than one superclasses are present.
So ambiguity or confusion arises, which superclass constructor gets called. That is the point, so it’s not possible. But in Interface, since there is no constructor, hence ambiguity issue will never arise.
Ambiguity while method execution.
Assuming that a subclass has more than one superclass and superclasses has similar methods that are method declaration is the same but method implementations are different and these methods are inherited to subclass.
So in a subclass, it becomes the method duplication. And also when we call or invoked such a method then ambiguity will arise while calling the method.
Thanks a lot for reading my article. It’s highly appreciated. I hope you got a chance to understand Interface in a different way. If you any queries or doubt please don’t hesitate to ask, I love to clear your all doubts.
And if you any suggestions regarding my posts Please let me know. If you want to learn some other concept of Java, please check out the articles list.