question on interface

Discussion in 'Java' started by syntax, Feb 15, 2004.

  1. syntax

    syntax Guest

    hi, i have some basic question on interface....

    question1
    ------------
    it is called that interfaces are similar to classes.

    so, as classes can produce objects by usuing "new" keywords, can
    interfaces do the same thing?

    or in other words, does interfaces produce "object like" things
    similar to classes?


    question2
    -----------

    suppose a class is "implementating" an interface....so the class is
    defining the method which is declared in the interface.

    i want to know what would be the possible return types of the
    following

    return type = ObjectOftheClass.MethodDeclaredIntheInterface()

    what would be return type? will it be a object of the class? or will
    it be a "object like" thing of the interface?(if it exists!)

    thanks
     
    syntax, Feb 15, 2004
    #1
    1. Advertisements

  2. syntax <> scribbled the following:
    > hi, i have some basic question on interface....


    > question1
    > ------------
    > it is called that interfaces are similar to classes.


    > so, as classes can produce objects by usuing "new" keywords, can
    > interfaces do the same thing?


    > or in other words, does interfaces produce "object like" things
    > similar to classes?


    Yes or no. You cannot directly instantiate interfaces with new, but
    you can instantiate classes implementing them. In other words, if
    you have:

    public interface IFoo {}
    public class Foo implements IFoo {}

    then this is an error:
    IFoo i = new IFoo();
    but this is OK:
    IFoo i = new Foo();

    > question2
    > -----------


    > suppose a class is "implementating" an interface....so the class is
    > defining the method which is declared in the interface.


    > i want to know what would be the possible return types of the
    > following
    >
    > return type = ObjectOftheClass.MethodDeclaredIntheInterface()


    > what would be return type? will it be a object of the class? or will
    > it be a "object like" thing of the interface?(if it exists!)


    The return type will be whatever the interface defines it to be. That
    the method is implemented in the class doesn't change that. Of course,
    the actual returned *object* might have another class, but the type
    is the same. (If it's a primitive, it'll be the same type, period.)

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "And according to Occam's Toothbrush, we only need to optimise the most frequent
    instructions."
    - Teemu Kerola
     
    Joona I Palaste, Feb 15, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrea Williams

    Interface, class or collection Question

    Andrea Williams, Feb 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    436
    Andrea Williams
    Mar 2, 2004
  2. Wildepiet
    Replies:
    0
    Views:
    1,998
    Wildepiet
    Jun 14, 2004
  3. cyberco
    Replies:
    8
    Views:
    662
    cyberco
    Feb 25, 2006
  4. Xah Lee
    Replies:
    15
    Views:
    813
    Sherm Pendley
    Mar 23, 2007
  5. Xah Lee
    Replies:
    15
    Views:
    831
    Sherm Pendley
    Mar 23, 2007
  6. Replies:
    4
    Views:
    472
    timjowers
    Nov 2, 2007
  7. rote
    Replies:
    0
    Views:
    1,343
  8. Xah Lee
    Replies:
    11
    Views:
    708
    Nigel Wade
    Mar 22, 2007
Loading...