Interfaces Versus Abstract Classes

     

There are differences between an interface and an abstract class, though commonly the two are confused . Why do you need an interface? When should I use an interface and when an abstract class? Let's let the two duke it out for themselves , and you decide.

Monsters fight!

Round One : An interface is a totally abstract class . In an abstract class, you can define some methods that are abstract, and some methods that are fully implemented. Any class that extends that abstract class must implement the abstract methods , but it inherits the functionality as implemented in the abstract class. This is very cool if you need that kind of structure in your program, but can make your API a little snaky.

Round Two : In an interface, there can be no implementation whatsoever. You can say this:

 

 List starWarsFigures = new ArrayList(500); 

Then, your reference type is the interface type (List)! That means that later in your code you can change the implementation without breaking any of the code that does something with your starWarsFigures object. Like this:

 

 List starWarsFigures = new Vector(); 

This is very cool if you need that kind of flexibility, which is often a good thing.

Round Three : An interface is less flexible in how its variables and methods are declared. Here are the rules about writing an interface.

Rules for Writing an Interface

  • Declare an interface using the keyword interface .

  • An interface may extend zero or more interfaces if it likes, but it cannot extend a class, because then it would inherit functionality, and interfaces cannot have functionality. They can only talk about it.

  • Interface methods cannot be static.

  • Interface methods are implicitly abstract. For that reason, you cannot mark them final (duh), synchronized , or native because all of these modifiers tell how you're going to implement the method, and you're voluntarily giving up that ability when you write the method in an interface.

  • strictfp is okay on an interface. It is okay because you can evaluate compile-time constants using strictfp rules within an interface.

  • All interface methods are implicitly abstract and public, regardless of what you write in the interface definition! It is true. The interface method declaration void biteIt(int times) , despite its apparent access level of default, actually has public access. Try it. If you write a class in another package beyond the visibility of default access, and include the seemingly legal implementation of void biteIt(int times) { ; } , the compiler will tell you that you cannot reduce the visibility of the method from public to default. They're all abstract; they're all public .

  • An interface can define variables. But all variables defined in an interface must be declared public , static , and final . Many Java programmers have adopted the practice of defining only variables within an interface and putting constants in it. This works to get at shared constants, but it is a workaround and is no longer necessary if you're using J2SE SDK 5.0. It features a new static import facility that allows you to import constants just as you would a class or package.

  • It should be obvious by now that an interface cannot implement another interface or a class.

  • You may modify your methods using the keyword abstract if you like, but it will have no effect on compilation. Methods in an interface are already abstract, and the Java Language Specification says that its use in interfaces is obsolete.

  • Likewise, the interface itself is already abstract. So you can do this if you want: public abstract interface Chompable {}. But there's no point; it's redundant.

  • Interfaces have default access by default (!). So this is legal: interface Chompable { } . But if you want your interface to have public access, use that modifier in the interface declaration.

  • You cannot declare an interface as having private access. It doesn't make any sense. No one could implement it. So private interface Chompable { } gets you a compiler error for your trouble.

  • public , static , and final are implicit on all field declarations in an interface.

There are some weird things to keep in mind.

Interfaces can be declared private or protected if they are declared nested inside another class or interface. The following will compile, though its usefulness is dubious at best.

 

 public class interface test {     private interface myinterface{ } } 

Only an inner class of the class of which the interface is declared can implement the interface.

Is and Does

Remember that the job of an interface is to designate a role that a class will play in the society of the application. Whereas a class ( especially an abstract class) defines what something is, an interface defines what it can do.

Which of the following would you make into an interface, and which would you make into an abstract class?

Person

Employee

Programmer

Skier

WildAnimal

DataAccessor

Swimmer

WestCoastChopper

You'll see a 3-dimensional pattern emerge if you relax your eyes and stare at it long enough. This sort of thing is important to keep in mind as you design your application. You can do all of your applications without ever using an interface. But again, they clarify your API and provide you with flexibility down the road.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net