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. Advertising

  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. Advertising

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. Wildepiet
    Replies:
    0
    Views:
    1,848
    Wildepiet
    Jun 14, 2004
  2. cyberco
    Replies:
    8
    Views:
    479
    cyberco
    Feb 25, 2006
  3. Xah Lee
    Replies:
    15
    Views:
    572
    Sherm Pendley
    Mar 23, 2007
  4. Xah Lee
    Replies:
    15
    Views:
    618
    Sherm Pendley
    Mar 23, 2007
  5. Replies:
    4
    Views:
    336
    timjowers
    Nov 2, 2007
Loading...

Share This Page