implementing-interfaces

Java-Interface

Interface

An interface declares a set of methods and their signatures. The methods that are declared in an interface should not have implementation codes. A class that makes use of an interface should provide the codes for the methods declared in that interface. An interface is similar to a class, but there are several important differences :

1) Methods should be only declared in an interface. In other words, the implementation codes for the methods should not be provided in an interface.
2) All methods that are declared in an interface are automatically public.
3) An interface does not have instance variables. But an interface can have constants. Such constants will be inherited by the class that implements the interface.

A class that implements an interface should provide the implementation codes for the methods declared in that interface. An interface may be implemented by different classes in different ways. This means that different classes may provide different implementation codes for the methods declared in the interface.

Structure of Interface

The general form of an interface is as follows :

interface
It shall be noted here that all the variables have constant values and all the methods have no body statements. Every variable in an interface is implicitly public, static and final, while every method in an interface is implicitly public and abstract. Here are some simple interfaces :

interfaces
Implementation of Interface

A class shall make use of an interface by implementing the same. A class that implements an interface will look as follows :

implement interface
If a class implements an interface, it should implement all of the methods declared in that interface. In other words, the implementing class should provide codes for all the methods declared in the interface. Otherwise the compiler will generate an error message at the time of compilation. The methods that implement an interface must be declared public. Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition.

Program

Implementing-InterfaceImplementing-Interface

Program Source

interface Hardware {
    
    void ram();
    void processor();
}

class IntelInspiron3000series  implements Hardware {

    public void ram()
    {
        System.out.println("4GB Single Channel DDR3L "); 
    }
    
    public void processor()
    {
        System.out.println("4th Generation Intel® Core™ i5"); 
    }
}

class IntelInspiron7000series implements Hardware {

    public void ram()
    {
        System.out.println("8GB Dual Channel DDR3L"); 
    }
    
    public void processor()
    {
        System.out.println("4th Generation Intel® Core™ i7"); 
    }
}

public class Javaapp {

    public static void main(String[] args) {
        
        IntelInspiron3000series intel3000 = new IntelInspiron3000series();
        System.out.println("Intel Inspiron 3000 series configuration");
        intel3000.ram();
        intel3000.processor();
        
        IntelInspiron7000series intel7000 = new IntelInspiron7000series();
        System.out.println("Intel Inspiron 7000 series configuration");
        intel7000.ram();
        intel7000.processor();
    }
}

Leave a Comment