Java Interfaces For Beginners With Examples
This tutorial will guide you on what are interfaces in Java. You will know how to create interfaces and use them in Java programs.Basics of Interfaces in Java
You can go through the following sections to learn about Java interfaces.Interfaces Concept in Java
Description:
Interfaces can frequently be mistaken for classes in Java. In any case, the laying out distinction between a class and an interface is that an interface contains abstract methods. A class can execute an interface to give definition or body to abstract methods while the alternate way is beyond the realm of imagination. An interface is actually similar to a reference type in Java. An interface alongside the abstract methods contains pre-characterized constants and static methods. In OOPS language, an interface characterizes the conduct a class carries out. Al the methods proclaimed abstract in the interface should be characterized by the carrying out class.Grammatically, one can discover numerous likenesses between an interface and a class. An interface can characterize however many methods on a case by case basis. Similar as a class, an interface is saved with interfaceName.java and the bytecode is handled as the interface name. class record. Notwithstanding, an interface like a class can’t be launched and doesn’t contain any constructors. The idea of various Inheritance can be carried out by interfaces as an interface can broaden numerous interfaces, which is unimaginable with classes. The factors characterized through interfaces are consistently static and last. The methods inside an interface are verifiably open. An interface without any methods is known as a labeling interface.java interfaces
Syntax:
The overall language structure to characterize an interface is similar as that of a class. Interfaces and the methods inside the interfaces are certainly abstract, along these lines, we don’t have to utilize the abstract watchword.The keyword used is “interface”:public interface testInterface
// 1. *** abstract methods
// 2. *** static methods
// 3. *** static and final variables
Java Interfaces Example Program:
The example below will help you visualize the implementation of interfaces.interface miInterface
{
public static void disp()
{
System.out.println("Static method in interface");
}
public void testPrint( );
}
public class jTest implements miInterface
{
public void testPrint()
{
System.out.println("Defining interface abstract method");
}
public static void main(String args[])
{
jTest o = new jTest();
o.testPrint();
miInterface.disp();
}
}
Therefore, the output is:Extend Interface in Java
Description:
While giving the body to the abstract methods in the class, the mark of the methods ought not to be changed. A class can carry out numerous interfaces simultaneously. An interface is stretched out to different interfaces similar to A parent class acquires a youngster class. The watchword utilized is “stretches out” to ensure the parent interface acquires the usefulness of the youngster interface. The model beneath manages numerous Inheritance through interfaces:example:
The example below shows how can you extend an interface:interface miInterface1
{
static void miMethod1()
{
System.out.println("Static method 1");
}
public void miMethod2();
}
interface miInterface2
{
static void miMethod2()
{
System.out.println("Static method 3");
}
public void miMethod4();
}
interface miInterface extends miInterface1, miInterface2
{
static int num = 10;
}
public class ifTest implements miInterface
{
public void miMethod2()
{
System.out.println("Method from interface 1");
}
public void miMethod4()
{
System.out.println("Method from interface 2");
}
public static void main(String args[])
{
ifTest o = new ifTest();
o.miMethod2();
o.miMethod4();
System.out.println(miInterface.num);
}
}
Output:What to remember is that the “interface” doesn’t approach static methods of its subinterfaces (i.e., miMethod1 and miMethod3).Recommended Post:
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order