-1

I can make abstract class equivalent to interface by putting all abstract methods within abstract class. Why did Java's designers choose to provide support for interfaces and single inheritance instead of multiple inheritance and abstract classes. What is the advantage?

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
Jagan
  • 4,649
  • 19
  • 60
  • 70
  • 1
    Did you search this site at all before asking this question? http://stackoverflow.com/questions/2124951/java-interface-abstract-classes-abstract-method – Mike Deck Oct 20 '10 at 02:49
  • 1
    It's _not_ a good look starting your question with a rant about your treatment in previous questions. It makes you sound like a whiner. FWIW, Mehrdad has given you exactly the right answer. – paxdiablo Oct 20 '10 at 02:52
  • Yes i should not have done that. – Jagan Oct 20 '10 at 02:55
  • 1
    From comments, I think he is asking "Why did Java's designers choose to provide support for interfaces and single inheritance instead of just multiple inheritance and abstract classes?" – Arnold Spence Oct 20 '10 at 02:59
  • @Arnold Spence: Yes exactly! Thanks for expressing my doubt. – Jagan Oct 20 '10 at 03:04

4 Answers4

4

At least for one reason (besides the conceptual differences between the two): you can implement multiple interfaces but you can only inherit from a single abstract class at most.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Java developers could have provided that facility to Abstract class itself.That is what i am asking... – Jagan Oct 20 '10 at 02:52
  • 1
    Nope multiple class inheritance is a long known problem with C++ (http://en.wikipedia.org/wiki/Diamond_problem), and Java is trying to solve the ambiguity by forbidding it. – dvhh Oct 20 '10 at 03:19
  • Please consider my above comment before voting. Arnold Spence has edited my question(that is what i want to know ). – Jagan Oct 20 '10 at 03:20
  • 1
    @Jagan: With the current design in Java, implementation (*i.e.* code) can only flow down to the class from a *single source*. This avoids the problems associated with multiple inheritance but removes the ability to implement more than one set of methods (contracts). By adding interfaces to a single inheritance design, you get most of the benefits of multiple inheritance without most of the problems. – Mehrdad Afshari Oct 20 '10 at 04:15
0

Class-based multiple inheritance is not supported by Java. You can only inherit from a single class, but you can implement multiple interfaces.

This becomes handy when you need to treat multiple classes polymorphicly when they have different inheritance trees.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

An interface is a contract between a class and its behavior. If a class implements an interface, it MUST provide implementation for the methods specified in the interface. There's a number of things that differentiate the two, however.

Check out the Oracle website for more on the differences between interfaces and abstract classes:

http://download.oracle.com/javase/tutorial/java/IandI/abstract.html

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
0

If you have multiple classes and you want to force that there would be some methods that should be common among them, irrespective of their behavior(implementation), Interface does that for you

Interface, is like a contract, which every other class which implements it has to follow

daydreamer
  • 87,243
  • 191
  • 450
  • 722