views:

417

answers:

6

Hi, I'm new to Java.
Why are abstract or interface classes created, or when should we use abstract or interface classes?

+7  A: 

Interface is used when you only want to declare which methods and members a class MUST have. Anyone implementing the interfaca will have to declare and implement the methods listed by the interface.

If you also want to have a default implementation, use abstract class. Any class extending the abstract class will have to implement only its abstract methods and members, and will have some default implementation of the other methods of the abstract class, which you may override or not.

--EDIT - forgot to mention, Earwicker reminded me

Finally, you can implement as many interfaces as you want, but only extend one class (being it abstract or not). Keep that on mind before choosing.

Samuel Carrijo
That's not the fundamental difference - you can make an abtract class that only has abstract methods.
Daniel Earwicker
In this case, why not use a interface?
Samuel Carrijo
I didn't design the language! :) You could in theory have some protected data fields in the abstract class, which you couldn't do with an interface.
Daniel Earwicker
So, it's like an implemented member ;)
Samuel Carrijo
+4  A: 

An abstract class is a class, that don't implement some of it's methods. Obviously it cannot be instanciated. You have to inherit from an abstract class and implement the abstract methods in another class.

Interfaces are no classes at all (so don't call them interface class). Interfaces define the signature of methods without any implementation. Also interfaces have no member-fields. If you implement an interface in an class you have to provide implementions for the methods provided by the interface.

It makes sense to define a generalized API for some stuff, that can have completely different implementations. Abstract classes are more useful for classes, that do mainly the same, but have some subtle differences. You can combine both approaches.

A good example is the collection-framework of the Java class-library. You have the interface List, that defines how Lists have to behave. Some implementations are for instance ArrayList and LinkedList. As they behave similar the stuff that works the same for both is implemented in the abstract class AbstactList, both inherit this.

Mnementh
A: 

Abstract classes are used when you are building an inheritance hierarchy. However, most inheritance hierarchies should not be too "deep" (i.e. too many levels of inheritance). Many object oriented design books will favor interfaces over inheritance (one book I read once quoted a developer as saying that "inheritance is the single coolest [object oriented] feature you won't implement"), as this allows classes to be assigned behaviors "by contract", where the contract is the interface.

It is worth noting samuelcarrijo's answer - if you want to have a default implementation of a method, you would have to use an abstract class that has a concrete implementation of the method to give it a default implementation. This default implementation can be overridden in child classes.

Hope this helps!

Scott Davies
+1  A: 

SamuelCarrijo seems to have answered this question well.

In addition for Java, some frameworks require an interface to work with. I'm thinking of (say) dynamic proxies, or some client/server proxying frameworks. This is because they use introspection on the object to determine methods implemented by the interfaces implemented by the object. So occasionally you have to implement an interface for an object where, perhaps, you wouldn't normally bother.

Note this reason for interfaces is specific to Java.

Brian Agnew
+6  A: 

The key difference is that you can implement multiple interfaces in a class, but only extend a single abstract class. This is because an abstract class can also define fields that store data, whereas an interface cannot.

Daniel Earwicker
Good point! I hadn't remembered that. Java (like C#), does not allow for multiple inheritance of classes, but will allow the implementation of as many interfaces as you want.
Scott Davies
A: 

See Interface is Basically a "Contract".When you are defining an interface you are defining a Contract.Where abstract classes are extended , interfaces are Implemented. lets consider an example.

public interface Friend { void hello(); }

Now you have defined a contract which says that any class which wants to implement Friend needs to provide a definition for method hello().

Here is an implementation

public class myFriend implements Friend { public void hello() println("Done"); }

Now myFriend has fulfilled the contract.Now the question is where should interfaces be used. Interfaces help you Define a behavior which must be implemented. Say you have a class A which defines some functionality. You want that other classes should use this class functionality only if they define particular behavior (methods ). You enforce this restriction in term of interface.

Duleb