Results 1 to 7 of 7
Thread: Interface variable to class
- 10-11-2008, 12:26 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Interface variable to class
I need to have a variable of the type of whatever class implements an interface.
For example:
Bar would be classThatImplementsFoo.PHP Code:public interface Foo{ public classThatImplementsFoo foo; } public class Bar implements Foo{ public Bar foo; }
How do I do this?Last edited by zill; 10-11-2008 at 12:34 AM.
- 10-11-2008, 12:30 AM #2
I'm not exactly sure what you are trying to do here.
But it seems like you want to use generics.
Usually, when I want an interface to know about a member variable, in the interface, I have
public <T> getFoo();
and in the implementing class, it has
private Baz aFoo
public <T> getFoo() { return aFoo;}
- 10-11-2008, 12:30 AM #3
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Or what do you mean?PHP Code:public class Bar implements Foo { ... }
- 10-11-2008, 12:38 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
No, I just lazily typed it in wrong. Obviously I wanted to use the interface.
I need to have an interface that has a variable of the type of the class which implements that interface. I'll call this pretend variable foo. So, if class Foo implements said interface, Foo would need to have a variable named foo of type Foo. Similarly, if class Bar implements said interface, Bar would need to have a variable named foo of type Bar. Similarly, if class T implements said interface, T would need to have a variable named foo of type T.
How do I do this?
- 10-11-2008, 12:48 AM #5
How about entering it properly. No lazy typing, so we can tell what you are trying to do
Have you looked at generics?
google for "java generics"
- 10-11-2008, 01:01 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
No, I can get around doing what I need. Generics is not what I need. I don't think I can actually do what I wanted to do.
Thank you.
- 10-11-2008, 03:29 AM #7
Similar Threads
-
Interface and Abstract Class
By kian_hong2000 in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:22 PM -
Interface Vs Abstract Class
By javarishi in forum New To JavaReplies: 5Last Post: 06-15-2008, 05:43 AM -
Class Reflection: Is it an interface
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:10 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM -
Class inside an Interface
By $hr!k@nt in forum New To JavaReplies: 1Last Post: 12-21-2007, 10:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks