Example 1: How to achieve multiple inheritance in Java using interfaces
KW.java
interface Male
{
    void male();
}

interface Female
{
    void female();
}

interface Category extends Male,Female
{
    void humanBeing();
}

class KW implements Category
{
    public void male() 
    {
        System.out.print("Male + ");
    }
    public void female() 
    {
        System.out.print("Female = ");
    }
    public void humanBeing() 
    {
        System.out.println("We are Human Beings");
    }
    public static void main(String[] args) 
    {
        KW k=new KW();
        k.male();
        k.female();
        k.humanBeing();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Male + Female = We are Human Beings kodingwindow@kw:~$
Example 2: How to achieve multiple inheritance in Java using interfaces
KW.java
interface WhiteBank
{
    void bonus();
}
interface GreenBank
{
    void gift();
}
class KW implements WhiteBank, GreenBank
{
    public void bonus()
    {
        System.out.println("WhiteBank Festival Bonus: 500");
    }
    public void gift()
    {
        System.out.println("GreenBank Festival Bonus: 1000");
    }
    public static void main(String args[])
    {
        KW k=new KW();
        k.bonus();
        k.gift();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW WhiteBank Festival Bonus: 500 GreenBank Festival Bonus: 1000 kodingwindow@kw:~$
Advertisement