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.

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. The general form of an interface is as follows :

interface InterfacefaceName {

    returnType methodName (Parameter);
}

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

class ClassName implements InterfaceName { }

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.

Program

0

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