Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Interface

Thread view: 
Ericsson - 17 Dec 2007 13:04 GMT
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???
Christian - 17 Dec 2007 13:22 GMT
Ericsson schrieb:
> Hi
> I am reading a book about Java . It says that :
[quoted text clipped - 4 lines]
>
> 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)
maxnesler@gmail.com - 17 Dec 2007 13:37 GMT
> Hi
> I am reading a book about Java . It says that :
[quoted text clipped - 4 lines]
>
> 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
}
Lew - 17 Dec 2007 15:31 GMT
>> Hi
>> I am reading a book about Java . It says that :
[quoted text clipped - 11 lines]
>
> 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 );
 }

Signature

Lew

Stefan Ram - 17 Dec 2007 16:21 GMT
>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.
Lew - 17 Dec 2007 16:47 GMT
>> An interface may not define any instance fields. Fields are an
>> implementation detail, and an interface is a pure specification
[quoted text clipped - 5 lines]
>   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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.