Académique Documents
Professionnel Documents
Culture Documents
In Java, under what circumstances would you use abstract classes instead of interfaces?
When you declare a method as abstract, can other nonabstract methods access it? In
general, could you explain what abstract classes are and when you might use them?
Those are all excellent questions: the kind that everyone should ask as they begin to dig
deeper into the Java language and object-oriented programming in general.
Yes, other nonabstract methods can access a method that you declare as abstract.
But first, let's look at when to use normal class definitions and when to use
interfaces. Then I'll tackle abstract classes.
With the Strategy pattern, you can simply encapsulate the algorithm behind an object. If
you do that, you can provide new media plug-ins at any time. Let's call the plug-in class
MediaStrategy. That object would have one method: playStream(Stream s). So to
add a new algorithm, we simply extend our algorithm class. Now, when the program
encounters the new media type, it simply delegates the playing of the stream to our media
strategy. Of course, you'll need some plumbing to properly instantiate the
algorithm strategies you will need.
Many developers forget that a class that defines an abstract method can
call that method as well. Abstract classes are an excellent way to create planned
inheritance hierarchies. They're also a good choice for nonleaf classes in class
hierarchies. Neither can be instantiated. An interface has all public members and no
implementation. An abstract class is a class which may have the usual flavors of class
members (private, protected, etc.), but has some abstract methods.Use interface as much
as possible and only use an abstract class in the case where you want to provide some
(but not all, of course)
implementation. In practice, you want to prefer using, passing and returning interfaces,
not abstract class references. I think even in the case where you have an abstract class to
share some implementation, you still want to have an interface represent what will be
used by the client (for an example look at the new Collections -- you often pass around
Collection, List, Set or Map references, but not a AbstractList, AbstractSet or
AbstractMap, references).
If you have an abstract class with no defined methods, you're better off using an
Interface.
1]A class must have atleast one abstract method in order to make the class an abstract
class whereas in an interface all mathods are abstarct by default.
2]An abstarct mathod in the abstarct class can have a body whereas no mathod in an
interface can have a body.
4] Best way to understand both as take an example of mammel & man. Mammel is an
abstarct class where certain properties of a man are defined.(Like sound)This sound
mathod will not have a body but man class will implement it as man speaks. Dog class
will implement it as dog barks.
Interface whereas just another class that works as a contract between classes.You and
your wife live together and your wife does not know how to clean the house(don't mind
bare example) so you can't create another relationship as java does not accept multiple
relationship.So make a contarct with a maid she knows her job but you / your wife will
once tell how you want her to clean your house. Here the maid is an interface.
An abstract class can grow whereas an interface cannot. When you inherit from an
abstract class and later on if you want to add a new method or memeber variable then
you can do that and all existing derived classes and their uses ( if you have already used
derived classes in your code ) are still valid ( of course, you will have to provide some
default implementation of new methods ). But if you have created and used an interface
then you cannot add a new method to that interface without rewritng the existing code.
Abstract classes are helpful when some of the behavior is defined for most or all
objects of a given type, but some behavior makes sense only for particular classes and not
a general superclass.
There are two important differences between interfaces and abstract classes.
These differences usually direct the choice of which is best to use in a particular
implementation. If multiple inheritance is important or even useful, interfaces are used.
Any major class you expect to be extended, whether abstract or not, should be
an implementation of an interface. Although this approach requires a little more work on
your part, it enables a
whole category of use that is otherwise precluded. For example, suppose we
had created an Attributed class instead of an Attributed interface with an AttributedImpl
implementation class.
Programmers who wanted to create new classes that extended other existing
classes could never use Attributed, since you can extend only one class. Because
Attributed is an interface,
programmers have a choice: they can extend Attributed Impl directly and avoid
forwarding, or, if they cannot extend, they can at least use forwarding to implement the
interface. You can even provide multiple possible implementations of the interface to
prospective users.
You can change an abstract class by adding more methods and your existing
code will not break. But once you specify an Interface, it is cast in stone and cannot
change without breaking all your code that uses it. There have been places in the Java
libraries where the Sun team used an Interface and regretted it later