[java] Can we create an instance of an interface in Java?

Is it possible to create an instance of an interface in Java?

Somewhere I have read that using inner anonymous class we can do it as shown below:

interface Test {
    public void wish();
}

class Main {
    public static void main(String[] args) {
        Test t = new Test() {
            public void wish() {
                System.out.println("output: hello how r u");
            }
        };
        t.wish();
    }
}
cmd> javac Main.java
cmd> java Main
output: hello how r u

Is it correct here?

This question is related to java interface instance inner-classes anonymous-class

The answer is


Yes we can, "Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name"->>Java Doc


Normaly, you can create a reference for an interface. But you cant create an instance for interface.


No in my opinion , you can create a reference variable of an interface but you can not create an instance of an interface just like an abstract class.


Yes it is correct. you can do it with an inner class.


Short answer...yes. You can use an anonymous class when you initialize a variable. Take a look at this question: Anonymous vs named inner classes? - best practices?


You can never instantiate an interface in java. You can, however, refer to an object that implements an interface by the type of the interface. For example,

public interface A
{
}
public class B implements A
{
}

public static void main(String[] args)
{
    A test = new B();
    //A test = new A(); // wont compile
}

What you did above was create an Anonymous class that implements the interface. You are creating an Anonymous object, not an object of type interface Test.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to interface

Cast object to interface in TypeScript When to use Interface and Model in TypeScript / Angular Is there a way to create interfaces in ES6 / Node 4? Can a normal Class implement multiple interfaces? When to use: Java 8+ interface default method, vs. abstract method How should I have explained the difference between an Interface and an Abstract class? When do I have to use interfaces instead of abstract classes? How to extend a class in python? Interface type check with Typescript Abstract Class vs Interface in C++

Examples related to instance

best way to create object __init__ and arguments in Python What exactly is an instance in Java? Can we create an instance of an interface in Java? How to create a list of objects? What is the difference between an Instance and an Object? The difference between Classes, Objects, and Instances Python object deleting itself

Examples related to inner-classes

Difference between final and effectively final Is not an enclosing class Java Nested or Inner Class in PHP Java - No enclosing instance of type Foo is accessible Can we create an instance of an interface in Java? Why would one use nested classes in C++? Getting hold of the outer class object from the inner class object Nested classes' scope? Java: Static vs inner class Why Would I Ever Need to Use C# Nested Classes

Examples related to anonymous-class

How to start anonymous thread class Multiple inheritance for an anonymous class Why are only final variables accessible in anonymous class? Can we create an instance of an interface in Java? How are Anonymous inner classes used in Java?