18

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...

BalusC
1.1m377 gold badges3.7k silver badges3.6k bronze badges
asked Dec 6, 2011 at 20:18

3 Answers 3

24

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>).

answered Dec 6, 2011 at 20:20
Sign up to request clarification or add additional context in comments.

1 Comment

How about optional operations, should you worry?
5

You cannot instantiate an interface. But you may type your variable with the interface name:

Myinterface foo = new MyObject();

Assuming MyObject implements MyInterface.

It may be useful when acquiring objects from an external source, for instance.
In such a case, you don't know (and don't care) about the real type of the object.

You only need to know and ensure it implements some interface, so you can call interface methods on the object.

Assuming you have the following variable:

Myinterface foo;

And two classes (Foo and Bar), both implementing MyInterface.
You will then be able to assign the foo variables with instances of both Foo and Bar.

The same applies to methods arguments, of course.

answered Dec 6, 2011 at 20:20

2 Comments

Thank you,Can you also answer my 2nd question, under what circumstance I should do this thing? Why do it like this? What is the benefit?
Thank you, both you and Adam give me a very clear explanation:)
1

You don't instantiate an interface. You create classes that implement the interface, instantiate those, but reference the interface (as opposed to the implementing class).

This allows your code to remain implementation-neutral, similar to using a List reference, but an ArrayList implementation.

When combined with dependency injection it makes testing and extensibility much easier, too.

answered Dec 6, 2011 at 20:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.