Can we create an object of an interface?

What you are seeing here is an anonymous inner class:

Given the following interface:

interface Inter {
    public String getString();
}

You can create something like an instance of it like so:

Inter instance = new Inter() {
    @Override
    public String getString() {
        return "HI";
    }
};

Now, you have an instance of the interface you defined. But, you should note that what you have actually done is defined a class that implements the interface and instantiated the class at the same time.

Leave a Comment