• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Interfaces Why Not SuperClassees  RSS feed

 
Sandeep Sirsekar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyPolymorphicMethod(InterfaceType in)

now cant we use SuperClass to use polymorphism why to use only Interface when we pass ImplementerClasses.can't we use, let's say

anyPolymorphicMethod(SuperClassType sc)

any body having any idea.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not duplicate threads.
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly clear what you are trying to ask.

But, if you are the one creating the method, you sure can do whatever you want. There is no restriction to say you have to pass by Interface only, you are allowed to make it an Class or inherited Interface if you incline to.

It's all depend the purpose of your design. In general it is better to pass interface. On some cases, it does necessary for the implementation to use class.

Some example is in Servlet API. If you notice the high level Servlet API define the methods using Interface such as ServletRequest and ServletResponse. But the lower lever API use more specific HttpServletRequest and HttpServletResponse.

I know..I know, HttpServletRequest is also an Interface. But it can be a class. What I want to emphasize is that on some cases you can pass a lower level interface/class as you see fit your needs.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a method it is always better to require as least parameters and as generic types as possible.

If your method only invokes "equals()" on passed parameter then it is best to use "Object" class. This allows broader use of your method.

Lack of interfaces in a non-trivial java application usually indicates poor design.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!