Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Saturday, July 23, 2016

Java 8 Features | Default Method For Interface

Default Method For Interface

There are some new features that are introduced into Java 8 and one of them is the default method

Interfaces in java are known to be fully abstract i.e. we can not define any method within an interface. But now in Java 8 we can define method inside an interface that is a default method.

It gives you a way to write default implementation for a method. Here is a example.

Code



interface SpeedStar {
    public String getName();

    public default void power() {
        System.out.println("I'm the fastest man alive!");
    }
}

class Flash implements SpeedStar {
    private String name;
    public Flash(String name) {
        this.name = name;
    }

    public String getName() {
        return "I'm " + name + ".";
    }
}

public class Demo
{
    public static void main(String[] arg) {
        Flash f = new Flash("Barry Allen");
        System.out.println(f.getName());
        f.power();
    }
}

Output

I'm super fast!
I'm Barry Allen.