Interface

E

Ericsson

Hi
I am reading a book about Java . It says that :

An interface may not define any instance fields. Fields are an
implementation detail, and an interface is a pure specification
without any implementation .

What does this mean???
 
C

Christian

Ericsson said:
Hi
I am reading a book about Java . It says that :

An interface may not define any instance fields. Fields are an
implementation detail, and an interface is a pure specification
without any implementation .

What does this mean???

The interface defines functions a class implementing the interface must
provide.

It does not specify variables that the instance of the class has, that
others may access directly.

These instance variables may be used to answer the implemented
functions... but other classes do not have to know anything about them.
"Information hiding" (google)
 
M

maxnesler

Hi
I am reading a book about Java . It says that :

An interface may not define any instance fields. Fields are an
implementation detail, and an interface is a pure specification
without any implementation .

What does this mean???

The interface will 'specify' the signature that every class will
implement. As in the parameters that get passed to each method.

A interface "method" looks like:

foo( Object 0, int i);

The class that implements it is very simalar:

void foo(Object 0, int i){

//do stuff with objects and an int
}
 
L

Lew

The interface will 'specify' the signature that every class will
implement. As in the parameters that get passed to each method.

A interface "method" looks like:

foo( Object 0, int i);

I can't tell in my font what the variable name is for the "Object" parameter,
but it isn't an upper- or lower-case letter 'oh'. I expect a lower-case 'o'
was intended.
The class that implements it is very simalar:

void foo(Object 0, int i){

//do stuff with objects and an int
}

The similarity to which maxnesler alludes is that the method signature is the
same - that's the part from "void" through the closing parenthesis.

What maxnesler forgot to show is that the implementing class declares the
method to be 'public', which is implied automatically in the interface
declaration but not so for the class declaration, and the class as
implementing the interface, the penalty for giving such an incomplete example.

package foo;
public interface Fooish
{
public void foo( Object o, int i );
}

Separate file:

package bar;
import foo.Fooish;
public class Foo implements Fooish
{
public void foo( Object o, int i )
{
// do stuff ...
}
}

The difference is that the interface has only a semicolon for a method "body",
but a class has to declare a real method body. Note that the class explicitly
'implements' the interface. This sets up an inheritance ("is-a") relationship
between the interface and the class. Now the interface can be used as the
abstract supertype for the implementation.

Some other file:

...
public void bax()
{
Fooish fooish = new Foo();
fooish.foo( new Object(), 17 );
}
 
S

Stefan Ram

Ericsson said:
An interface may not define any instance fields. Fields are an
implementation detail, and an interface is a pure specification
without any implementation .

An interface declaration introduces a new reference type whose
members are classes, interfaces, constants and abstract
methods. This type has no implementation, but otherwise
unrelated classes can implement it by providing
implementations for its abstract methods.
 
L

Lew

Stefan said:
An interface declaration introduces a new reference type whose
members are classes, interfaces, constants and abstract
methods. This type has no implementation, but otherwise
unrelated classes can implement it by providing
implementations for its abstract methods ...

and by creating an inheritance relation to the interface via the 'implements'
keyword.
 

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,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top