question on interface

S

syntax

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
 
J

Joona I Palaste

syntax said:
hi, i have some basic question on interface....
question1
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 ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top