difference between Interfaces and class

E

Eric

Dear all:

I can't figure out the difference between Interfaces and class on java.

Best Regards!
Eric Man
 
T

Tim Slattery

Dear all:

I can't figure out the difference between Interfaces and class on java.

An interface just gives the signatures for functions. Any class that
claims to implement that interface must actually implement all the
functions that the interface describes.
 
J

Jeffrey Palm

Tim said:
An interface just gives the signatures for functions. Any class that
claims to implement that interface must actually implement all the
functions that the interface describes.

Not exactly true -- a class can implement an interface and not implement
all the functions and be abstract.

Jeff
 
A

Anton Spaans

Also, which follows from Tim's description:

An interface can not be instantiated:
public interface MyIF
{
...
...
}

MyIF instance = new MyIF(); // error.

However, classes implementing (one ore more) interfaces can:
public class MyClass implements MyIF
{
...
...
}

MyIF instance = new MyClass(); //OK.

An interface can also be described as a contract between a class
implementing it and a user/caller of that class. The caller knows what to
expect and the callee guarantees that the contract (interface description)
is met.
To avoid multiple inheritance head-aches, interfaces can not define member
variables and can not define method-implementations. Interfaces just
*declare* methods.

Another use of interface are the so-called *marker* interfaces. A marker
interface promises the user/caller that it has a certain behavior that can
not be declared in Java, but that is defined by implementation(/code) of an
implementing class. An example is the java.io.Serializable. These interface
declare not methods at all.. they look empty:

public inteface MyMarker
{ }


-- Anton.
 
V

VisionSet

Eric said:
Dear all:

I can't figure out the difference between Interfaces and class on java.

Interfaces are the highest level of abstraction, they define what an object
of that type can do, but not how it does it.
The next level is an abstract class, these partially define behaviour and
leave some methods to be defined by subclasses.
Concrete classes are the lowest level, and only these can be instantiated.

The hierachy can be put together as such

interface Cutter {
void cut();
void sharpen();
}

abstract class Knife implements Cutter

abstract public void cut();

public void sharpen() {
// apply whetstone
}
}

class Penknife extends Knife {

public void cut() {
// do some whittling etc
}
}

Typically having the above structure you would then do something like this:

Cutter cutter = new Penknife();

By passing references to Cutter about one can ensure maximum generallity and
if ever you need to change the Concrete implementation it is simply a case
of changing the code at the point of instantiation.
 

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

Forum statistics

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

Latest Threads

Top