Example 1: How to achieve multiple inheritance in Java using interfaces and default methods
KW.java
interface WhiteBank
{
    default void bonus()
    {
        System.out.println("WhiteBank Festival Bonus: 500");
    }
}
interface GreenBank
{
    default void gift()
    {
        System.out.println("GreenBank Festival Gift: 1000");
    }
}
class KW implements WhiteBank, GreenBank
{
    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 Gift: 1000 kodingwindow@kw:~$
Example 2: How to achieve multiple inheritance in Java using interfaces and default methods
KW.java
interface WhiteBank
{
    default void bonus()
    {
        System.out.println("WhiteBank Festival Bonus: 500");
    }
}
interface GreenBank
{
    default void bonus()
    {
        System.out.println("GreenBank Festival Bonus: 1000");
    }
}
class KW implements WhiteBank, GreenBank
{
    public void bonus()
    {
        WhiteBank.super.bonus();
        GreenBank.super.bonus();
    }
    public static void main(String args[])
    {
        KW k=new KW();
        k.bonus();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW WhiteBank Festival Bonus: 500 GreenBank Festival Bonus: 1000 kodingwindow@kw:~$
What Next?
Java Collections
Advertisement