This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces Vs Abstract Classes

 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that a superclass declared as abstract has abstract methods in it, and a subclass which extends should either override all methods present in the superclass or itself be declared as abstract...
Further, In interfaces you have method definitions, which should be
implemented(all methods should be implemented by using public keyword)

Now My question???
i dont find any difference between the structure of abstract classes and interfaces(If you find one , let me know)???
Then why cant you only have either one of the two???
Why does java give you both abstract classes and interfaces???
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic has been discussed on a regular basis here at the Ranch. You should use the search tool to find the previous threads.

With that said, I'd like to clarify that an abstract class doesn't *have* to have abstract methods. You can give definitions for every method in a class that is declared as abstract. On the other hand, even a single abstract method implies that the enclosing class is abstract, even if it isn't explicitly declared as such. Typically, an abstract class has both abstract methods and non-abstract methods. In contrast, all methods in an interface are abstract. You cannot have any executing code in an interface.

Also, remember that you can only extend a single class. However, you can implement more than one interface. This is Java's simplification of multiple inheritence that is available in other Object Oriented languages, like C++ and SmallTalk.

Anyway, I won't go into any more detail since you should be able to find all the information you need by using the search tool (see the link I gave above). Good luck and let us know if you have any more questions.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic