Movatterモバイル変換


[0]ホーム

URL:


Translate:
  • Follow Us

  • Pages

  • Find a Job
  • DFP-300×250-1

  • Newsletter Subscription

      Email

      Country


    • Jobboard

    • Can an interface be instantiated in Java?

      No, an interface can not be instantiated in Java. So, if you have an interface called SomeInterface, then the following code will never compile:

      SomeInterface s = new SomeInterface( );

      However, because an interface is atype, you are allowed to write a method with a parameter of an interface type. And that method parameter will accept – as an argument – any class that implements the interface. If that’s confusing, just take a look at the example below. Suppose we have an interface called SomeInterface, and a method called SomeMethod defined in a class called SomeClass:

      public interface SomeInterface {  /* some code here...*?}public class SomeClass implements SomeInterface{  public void SomeMethod(SomeInterface s )  {    /* some code here */      }}

      In the code above, you can see that the method SomeMethod will accept an argument of type SomeInterface. And, any class that implements the SomeInterface interface can be passed in as an argument, because of the fact that the class would be of the SomeInterface type.

      Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

      Subscribe to our newsletter for more free interview questions.

      Follow @programmerintvw
      Previous...
      Next...

      Would you like to thankProgrammerInterview.com for being a helpful free resource?Then why not tell a friend about us, orsimply add a link to this page from your webpage using the HTML below.

      Link to this page:

      Please bookmark with social media, your votes are noticed and appreciated:


      [8]ページ先頭

      ©2009-2025 Movatter.jp