Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

abstract vs interface 2

Status
Not open for further replies.

fmardani

Programmer
Jul 24, 2003
152
What is the difference between an abstract and an interface?
Thanks
 
1. an interface only contains empty methods (method declarations), which must be implemented in the classes that implement the interface - it provides no code. An abstract class may contain code in it's methods or functions.

2. a class can implement multiple interfaces while it can only extend one abstract class

3. an interface can be added to a third party class while a third party class has to be rewritten to extend the abstract class

4. talking about speed, interfaces are slower that abstract classes because they require extra redirection to the actual implemented methods

5. extending your code in an interface would mean that you have to find all the classes that implement it and make the necessary modifications there, while adding new methods or variables to an abstract class won't hurt any extension of it.


there are alot more differences, but this should give you the general idea

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Could you please elaborate on point number 5?
Thanks
 
take a look at number 1. it says that interfaces contain only signatures of methods that need implementations (actual bussiness logic written) in the classes that implement the interface. an abstract class can have methods that already contain the code inside.

so if you need to extend the interface (add a new method) you would have to go through all the classes that implement your interface and declare the method there (even if you leave it empty)

while if you decide to extend an abstract class because you found an extension of it to require this, you simply modify it (whether by adding a method - with or without code - or by adding attributes) and nothing needs to be changed in any of the classes extending it.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
This is now clear.
Thanks for your time
 
DaZZleD very good explanation. You should get a star for this, here you go...

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top