Java Interfaces for Beginners Explained with Examples – MPS

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 For Beginners With Examplesjava 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:Java Interfaces Example Program:

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:Extend Interface in JavaWhat to remember is that the “interface” doesn’t approach static methods of its subinterfaces (i.e., miMethod1 and miMethod3).

Recommended Post:

Source link

Leave a Reply

%d bloggers like this: