Results 1 to 7 of 7
  1. #1
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default apply interface to a different class

    say, I have a client code I want to implement

    UserStuff us = new UserStuff();

    boolean result = us.isChrome();

    isChrome method is an interface method of CheckBrowser class.

    Is it possible to implement like this using Factory method pattern or something.
    Of course I need to add necessary methods like getter and setter or some else.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: apply interface to a different class

    Maybe someone smarter than me will figure it out, but I don't think there's nearly enough information there for you to get meaningful help. You may need to supply more context.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: apply interface to a different class

    I'm not smarter and I did not figure it out, but the answer to the question is easy: yes. The reason is simple: anything is possible.

    That's why that particular question is not interesting at all. The real question is: is it a good idea? To be able to answer that, the example needs to be made a whole lot more clear.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default Re: apply interface to a different class

    thanks for the replies. I understand that it does not provide enough information.
    Only thing that is clear at this point is the client part.
    What confuses me most is that "isChrome" is not a member of the class "UserStuff".
    then how can it be possible to achieve such implementation of the method to an object of "UserStuff"???

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: apply interface to a different class

    Still no idea what you're talking about. But it seems like your understanding of classes, interfaces and objects simply needs a bit of freshening up.

    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default Re: apply interface to a different class

    right. it is my understanding. I need clarify that.

    the only methods that an object can call are those defined in the same class? Am i misunderstanding?
    Last edited by soichi; 08-20-2013 at 10:47 AM.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: apply interface to a different class

    Quote Originally Posted by soichi View Post
    What confuses me most is that "isChrome" is not a member of the class "UserStuff".
    then how can it be possible to achieve such implementation of the method to an object of "UserStuff"???
    First, do you have access to the class code UserStuff? If so, then implement the interface or
    extend the class and implement the interface. In the subclass case, only the subclass will know about
    isChrome.

    Or write a wrapper to the class UserStuff and implement the interface on the wrapper. Then when the isChrome
    method is invoked, use the state of the UserStuff object to return whatever is meaningful. Of course, the latter
    method does not permit passing an instance of the wrapper class to some method that expects a UserStuff object.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  2. Replies: 15
    Last Post: 02-04-2012, 11:15 PM
  3. Replies: 14
    Last Post: 05-28-2009, 05:44 AM
  4. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  5. Class inside an Interface
    By $hr!k@nt in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 10: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
  •