Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable of Interface type

Tags:

java

interface

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

like image 623
Mellon Avatar asked Dec 06 '11 20:12

Mellon


People also ask

Can you have a variable of an interface type?

You can declare variables to be of an interface type, you can declare arguments of methods to accept interface types, and you can even specify that the return type of a method is an interface type.

What kind of variables do interfaces have?

The interface contains the static final variables. The variables defined in an interface can not be modified by the class that implements the interface, but it may use as it defined in the interface. đź”” The variable in an interface is public, static, and final by default.

Can we define variable in interface?

No you can not declare variable in interface. No, we can't declare variables, constructors, properties, and methods in the interface. no,interface cannot contains fields.

What is default type of variables in an interface?

In an interface, variables are static and final by default. All variables in an interface in java should have only public access modifier. For example, here is a string variable “shapes” declared in the interface.


2 Answers

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

like image 144
Adam Zalcman Avatar answered Oct 10 '22 07:10

Adam Zalcman


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.

like image 33
Macmade Avatar answered Oct 10 '22 08:10

Macmade