-8

I came across few design constructs in a large application:

  1. independent use of abstract classes
  2. independent use of interfaces
  3. abstract class implementing an interface
  4. interface including another interface

Being new to java, I really would appreciate if anyone could exactly pinpoint the leverage/advantage of one over the other in specific situations. What we miss if we dont use such constructs?

Shan
  • 5,054
  • 12
  • 44
  • 58
  • 12
    Are you sure you already met the fourth point? – sp00m May 21 '13 at 10:15
  • 2
    I dont think number 4 is possible. For the rest - it just depends. Generally speaking, all are fine in practise, depending on the circumstances. – vikingsteve May 21 '13 at 10:19
  • 2
    Related (even duplicated?): http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo http://stackoverflow.com/questions/10040069/abstract-class-vs-interface-in-java http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class – sp00m May 21 '13 at 10:21

1 Answers1

3
  1. Using an abstract class, you can have functions, that don't need to get implemented by the classes inheriting the abstract class. (look http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

  2. Using an interface, every subclass has to define every method, provided by this interface. (look http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

  3. Using an abstract class, implementing an interface is basically useless, if you don't need that interface for any other class, except this one abstract class. If the abstract class is not the only class implementing the interface, it can make sense to use.

  4. Is not possible

  5. Is possible... but again does only make sense, if you need that base interface not only for one other interface

Uroc327
  • 1,379
  • 2
  • 10
  • 28
  • 4. is not possible, 5. works. i.e. `public interface List extends Collection` – Marco Forberg May 21 '13 at 10:25
  • Abstract class implementing an interface can be a good pattern if many implementations will share common code, but some implementations may need to be substitutable for other classes (and can thus not derive from the abstract class). In that scenario, one should generally avoid using the abstract class type as anything other than a base type to derive from. – supercat May 22 '13 at 19:54
  • @Marco Forberg have a look at the example in my other post http://stackoverflow.com/questions/16746334/java-interface-extending-an-abstract-class and let me know if you still stick with your statement about no 4. If you think your statement is wrong, do modify / or rephrase your comment. – Shan Jun 06 '13 at 20:11
  • You already have the answer in that other question and at least eclipse should have warned you about that – Marco Forberg Jun 07 '13 at 06:32