GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  J2EE  >  Java

 Print  |  
Question:  When to use Interface over abstract class?

Answer: Abstract Classes: Classes which cannot be instantiated. This means one
cannot make a object of this class or in other way cannot create
object by saying  ClassAbs abs = new ClassAbs(); where ClassAbs is
abstract class.

Abstarct classes contains have one or more abstarct methods, ie method
body only no implementation.

Interfaces: These are same as abstract classes only difference is we
an only define method defination and no implementation.

When to use wot depends on various reasons. One being design choice.

One reason for using abstarct classes is we can code common
functionality and force our developer to use it. I can have a complete
class but I can still mark the class as abstract.
Developing by interface helps in object based communication.



May 05, 2008 09:05:38 #8
 Tins30   Member Since: May 2008    Total Comments: 3 

RE: When to use Interface over abstract class?
 
"adarsh83 wrote:

When we do not want any one to create object of our class we define the class as abstract.
when we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will."

That will still leave me wondering... When would someone not want to create object of some class? You also mentioned about when to define an interface.. But same is true for abstract class.. We can use abstract class to do exactly same thing that you
stated for interface.. Plz correct me if I am wrong.

Thanx
Tins30
     

 

Back To Question