Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:48+00:00 2026-05-27T09:18:48+00:00

I am learning Java, I saw the following description regards to interface in a

  • 0

I am learning Java, I saw the following description regards to interface in a book:

When a variable is declared to be of an interface type, it simply
means that the object is expected to have implemented that interface.

What does it mean? If I define an interface:

public interface Myinterface{

   void method_one(); 
   int method_two();
}

Then, I declare a variable to be of an interface type for example:

Myinterface foo = new Myinterface();
  • How the interface get implemented??
  • Under what circumstance I should define a interface type variable?

I completely get confused by the book’s description…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:18:49+00:00Added an answer on May 27, 2026 at 9:18 am

    You cannot instantiate an interface, i.e. you cannot do

    MyInterface foo = new MyInterface(); // Compile-time error.
    

    What you can do is instantiate a class that implements the interface. That is, given a class MyClass

    public class MyClass implements MyInterface {
      // ...
    
      @Override
      void method_one() {
        // ...
      }
      @Override
      int method_two() {
        // ...
      }
    }
    

    you can instantiate it and place a reference to it in your variable like this:

    MyInterface foo = new MyClass();
    

    If you had another class implementing MyInterface

    class MyClass2 implements MyInterface {
      // ...
    }
    

    you can also substitute a reference to this class’s instance under your variable:

    MyInterface foo = new MyClass2();
    

    This is where the power of interfaces lies: they define types, not a particular implementation and let you refer to any implementation of a given type.

    It is a very good programming practice to make classes implement interfaces and to use these to refer to instances of these classes. This practice facilitates a great deal of flexibility and reuse.

    Therefore, you should use interface type arguments and variables whenever it is conceivable that different implementations may be passed into the method you’re implementing. For example, if you’re working with a HashSet<T> instance, you should use a variable of type Set<T> to refer to it (class HashSet<T> implements interface Set<T>).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning java when i came across interface, I saw the following
Still in the early stages of learning Java and I saw the following in
I'm learning java and I have made simple program that simply reads value from
I'm learning Java, and the book I'm reading has the following example on cloning.
I'm just learning java and following a book. I have a program written via
So I'm learning java, and I have a question. It seems that the types
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
I'm learning Java by reading the online Java book, but im finding it hard
I'm currently learning Java, using a combination of the Head First Java book and
Learning java server technologies, trying to clarify some things. There are few technologies that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.