Results 1 to 7 of 7
  1. #1
    zill is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default Interface variable to class

    I need to have a variable of the type of whatever class implements an interface.

    For example:
    PHP Code:
    public interface Foo{
        public classThatImplementsFoo foo;
    }
    
    public class Bar implements Foo{
         public Bar foo;
    }
    Bar would be classThatImplementsFoo.

    How do I do this?
    Last edited by zill; 10-11-2008 at 01:34 AM.

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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;}

  3. #3
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    PHP Code:
    public class Bar implements Foo { ... }
    Or what do you mean?

  4. #4
    zill is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    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?

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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"

  6. #6
    zill is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

Similar Threads

  1. Interface and Abstract Class
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 03:22 PM
  2. Interface Vs Abstract Class
    By javarishi in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 06:43 AM
  3. Class Reflection: Is it an interface
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:10 PM
  4. Replies: 0
    Last Post: 04-02-2008, 08:04 AM
  5. Class inside an Interface
    By $hr!k@nt in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •