Can we instantiate interface in java if yes how we do it

Can we instantiate interface

Answer: You can not instantiate an interface in java. However, refer to an object that implements an interface by the type of the interface. For example:

Interface:

public interface TestInterface {

    public void methodTest();

}

 

Interface implementation:

package com.javahonk.markerinterface;

public class ImplementsTestInterface implements TestInterface {

    public static void main(String[] args) {
	TestInterface testInterface=new ImplementsTestInterface();
	testInterface.methodTest();
    }

    public void methodTest() {
	System.out.println("TestInterface Method");

    }

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *