Results 1 to 5 of 5
Thread: Interface as a type!
- 03-09-2009, 03:29 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Interface as a type!
I have a question about java interface. Assume that:
public interface intf_X{
// abstract methods define
}
Since intf_X can be used as a type (e.g. intf_X x1;) can we use intf_X as a return type from a function (I mean: intf_X foo()) and in which case we can use it? Can you give a simple example?
Thanks
-
the best test is already in your hands: your friendly Java compiler!
Why not create a simple interface and see if you can return it from a method? See if it compiles?
- 03-09-2009, 03:37 AM #3
Agree ^
Its best if you discover it first hand.
- 03-10-2009, 03:12 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Hello all,
I did the following test and gain a bit understanding of interface. It compiles just fine.
public interface X_intf{
void X_method();
}
public class X_impl{
// Constructor
public X_impl(){
// Do something
}
void X_method(){
// Do something
}
}
public interface MyInterface{
X_intf getInfo();
}
public class MyClass implements MyInterface(){
X_intf x;
// Constructor
public MyClass(X_intf x){
this.x = x;
}
public X_intf getInfo(){
return x;
}
}
// in Main.java
X_impl x_var = new X_impl();
MyClass var1 = new MyClass(x);
System.out.println(var1.getInfo().X_method());
- 03-10-2009, 07:03 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
i can't understand using interface as a type
By sireesha in forum New To JavaReplies: 3Last Post: 11-20-2007, 10:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks