Implement multiple interfaces in Java


Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signatures, no body, see: Java abstract method). A class can extend the features of other classes by use of inheritance. When a class extending more than one class is known as multiple inheritance. But Java doesn’t allow it because it creates the diamond problem and too complex to manage. We can achieve the multiple inheritances by use of interface.

Source Code

//How Multiple inheritance can be achieved by implement multiple interfaces
class Phone
{
    void voiceCall()
    {
        System.out.println("Make VoiceClass");
    }
    void sms()
    {
        System.out.println("We Can send SMS");
    }
}
interface Camera
{
    void click();
    void record();
}
 
interface player
{
    void play();
    void pause();
    void stop();
}
 
class SmartPhone extends Phone implements Camera,player
{
 
    @Override
    public void click() {
        System.out.println("Take a Selfi");
    }
 
    @Override
    public void record() {
        System.out.println("Take a video");
    }
 
    @Override
    public void play() {
        System.out.println("Play Music");
    }
 
    @Override
    public void pause() {
        System.out.println("Pause Music");
    }
 
    @Override
    public void stop() {
        System.out.println("Stop Music");
    }
}
 
public class interfaceDemo2 {
    public static void main(String[] args) {
        SmartPhone o =new SmartPhone();
        o.voiceCall();
        o.sms();
        o.click();
        o.record();
        o.play();
        o.pause();
        o.stop();
    }
}
 

Output

Make VoiceClass
We Can send SMS
Take a Selfi
Take a video
Play Music
Pause Music
Stop Music
To download raw file Click Here
`

Basic Programs