• Post Reply Bookmark Topic Watch Topic
  • New Topic

need of abstract class/interfaces?  RSS feed

 
Sri Dharan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
Why should we need extend abstract class/ implement interfaces?

regards
sri
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces are not classes, but more of a 'contract' of intented implementation which would be common to all classes implementing the interface. More specifically it is a broad design of behaviour which is too generic to instantiate on ts own, but is suited to individual implementations. For example a common behaviour of most animals would be to eat, but what and how to eat is different for many animals. If we expressed this in terms of Java, Animal would be the interface with an abstract method eat. Then any class implmenting the interface, would not only be considered an animal but would have to implement the eat method.

In this way, the interface maps the behviour that all animals (classes of) should provide, but leaving the specifics of implementation to the individual classes themselves.





The other beneit of Interface implementation is that any implementing class is considered an Animal and therefore may be passed to any behaviour expecting an Animal. For example a list or an Array of Animals. In this way code can be efficient, and loosley coupled, and anyone who wishes to use the Array, only needs to ensure the class implements the Animal interface. This ensures that you dont need to write loads of different cow or cat classes with different behaviours .

Abstract classes roughly work in the same way, but with a different perspective on the abstraction, as Abstract classes may have non Abstract methods, and only the Abstract methods need to be implemented by the extending class (unless that class itself is abstract). A drawback to the abstract class method is that as it is a class, extending classes, can only extend one superclass, wheras in an interface many interfaces may be implement.

Because of this level of abstraction and the function of interfaces, it would not ake sense to be able to instantiate interfaces, as it would simply be the same as a class, and generic abstraction could not work, in the polymorphic way it does, thus making coding far more complex and granular.

Thats my take, Im certain thaere are other memory based reasons and technical reasons why, but Ill leave that to the experts to describe.

Hope this helps
 
Sri Dharan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you stephen

regards
sri
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!