Interfaces In Java
This post will cover Java interfaces with examples – An effort to assist you in discovering something new.
Java Interfaces
When it comes to Java interfaces, using interfaces is another technique to accomplish abstraction.
A totally “abstract class” called an interface is used to group related methods with empty bodies:
Example
interface Bank{ // This is an interface of a Bank
public void bank_name(); // This is an interface method contain no body
public void bank_employees(); // Another empty body method is declared here
}
interface Car{ // This is an interface of a Carpublic void car_company(); // This is an interface method that does not contain a body
public void car_model(); // Another empty body method is declared here
}
The interface must be “implemented” (kind of like inherited) by another class using the implements keyword (instead of extends) in order to access the interface functions .
When it comes to Java interfaces, the “implement” class offers the interface method’s body:
interface SmartPhone{public void camera(); // interface method is declared here
public void calling(String name); // another interface method is declared here}class SmartPhones_Applications implements SmartPhone{ // new class inherits from SmartPhone class using the implements keywordpublic void camera(){ // Body of first interface method is declared here
System.out.println("Camera: 108 Megapixels (MP)");
}
public void calling(String name){ // Body of second interface method is declared here
System.out.println("Enter contact name to make a call : "+name);
}}public class Main {
public static void main(String[] args) {SmartPhones_Applications my_object=new SmartPhones_Applications();
my_object.camera(); // Calling interface method 1
my_object.calling("Robert"); // calling interface method 2}
Another approach.
interface Programming_languages{public void java_founder();
public void java_developers();}class Java implements Programming_languages{public void java_founder(){
System.out.println("Java was originally developed by James Gosling at Sun Microsystems");
}public void java_developers() {
System.out.println("There are exactly 9,007,346+ Java developers in the world");
}
}public class Main {
public static void main(String[] args) {Java myobj=new Java();
myobj.java_founder();
myobj.java_developers();
}
}
Considerations Regarding Interfaces:
- Just like abstract classes, an interface cannot be used to construct objects; for instance, in the example given above, a “Programming_languages” object cannot be created in the Main Class.
- Interface methods lack a body; instead, the “implement” class supplies one.
- You must override each one of an interface’s methods when implementing it.
- By default, interface methods are public and abstract.
- By default, public, static, and final interface attributes are used.
- A constructor is incompatible with an interface (as it is not capable of creating objects).
When Should Interfaces Be Used?
- To increase security, hide some information and only display key information about an object (interface).
- “Multiple inheritance” is not supported by Java (a class can only inherit from one superclass). However, if it comes to Java interface, the class can implement numerous interfaces, so it is achievable with interfaces.
Reminder: Use commas to separate each interface you want to use, to invoke multiple interfaces (see example below).
Multiple Interfaces
Use a comma to separate multiple interfaces while implementing them:
interface First_Interface{
public void first_Interface_Method();
}interface Second_Interface{
public void second_Interface_Method();
}class Third_Interface implements First_Interface,Second_Interface{public void first_Interface_Method() {
System.out.println("This is my first Interface method");
}public void second_Interface_Method() {System.out.println("This is my second Interface method");
}
}public class Main {
public static void main(String[] args) {Third_Interface obj=new Third_Interface();
obj.first_Interface_Method();
obj.second_Interface_Method();}// Output 1: This is my first Interface method
// Output 2: This is my second Interface method
}
interface Interface1{public void product(int mrx,int ample);}interface Interface2{public void division(int mrx,int ample);}class Implementing_Class implements Interface1,Interface2{public void product(int mrx,int ample){
System.out.println("Product of "+mrx+" x "+ample+" = "+(mrx*ample));
}public void division(int mrx,int ample){
float result=(float)mrx/ample;
System.out.println("The Result of "+ mrx + " / "+ample+" is = "+result);
}}public class Main {
public static void main(String[] args) {Implementing_Class my_object=new Implementing_Class();
my_object.product(12,8);
my_object.division(34,5);}// Output 1: Product of 12 x 8 = 96
// Output 2: The Result of 34 / 5 is = 6.8
}